Create WordPress Page that redirects to another UR

2020-05-19 04:27发布

I wanted to create a new WordPress page that is actually a link to another site. The goal is to have the page show up in a list of my pages, but actually send the web user to the target URL.

For example, say I want to include a page that indicates "My Photos" but actually redirects them to Flickr.

I'm guessing one way to accomplish this is by using a custom template page with a redirect instruction in PHP, but unfortunately I am a newbie to PHP and am not familiar with the way to accomplish this...

标签: php wordpress
7条回答
ゆ 、 Hurt°
2楼-- · 2020-05-19 04:55

I'm not familiar with Wordpress templates, but I'm assuming that headers are sent to the browser by WP before your template is even loaded. Because of that, the common redirection method of:

header("Location: new_url");

won't work. Unless there's a way to force sending headers through a template before WP does anything, you'll need to use some Javascript like so:

<script language="javascript" type="text/javascript">
document.location = "new_url";
</script>

Put that in the section and it'll be run when the page loads. This method won't be instant, and it also won't work for people with Javascript disabled.

查看更多
小情绪 Triste *
3楼-- · 2020-05-19 05:03

There are 3 ways of doing this:

  1. By changing your 404.php code.
  2. By using wordpress plugins.
  3. By editing your .htaccess file.

Complete tutorial given at http://bornvirtual.com/wordpress/redirect-404-error-in-wordpress/906/

查看更多
可以哭但决不认输i
4楼-- · 2020-05-19 05:05

Alternately, use a filter.

Create an empty page in your WordPress blog, named appropriately to what you need it to be. Take note of the post_id. Then create a filter that alters its permalink.

add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
    global $post;
    if ($post->ID == your_post_id_here) {
        $permalink = 'http://new-url.com/pagename';
    }
    return $permalink;
}

This way the url will show up correctly in the page no funny redirects are required.

If you need to do this a lot, then think about using the custom postmeta fields to define a postmeta value for "offsite_url" or something like that, then you can create pages as needed, enter the "offsite_url" value and then use a filter like the one above to instead of checking the post_id you check to see if it has the postmeta required and alter the permalink as needed.

查看更多
别忘想泡老子
5楼-- · 2020-05-19 05:08

I've found that these problems are often best solved at the server layer. Do you have access to an .htaccess file where you could place a redirect rule? If so:

RedirectPermanent /path/to/page http://uri.com

This redirect will also serve a "301 Moved Permanently" response to indicate that the Flickr page (for example) is the permanent URI for the old page.

If this is not possible, you can create a custom page template for each page in question, and add the following PHP code to the top of the page template (actually, this is all you need in the template:

header('Location: http://uri.com, true, 301');

More information about PHP headers.

查看更多
再贱就再见
6楼-- · 2020-05-19 05:09

I found a plugin that helped me do this within seconds without editing code:

https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

I found it here: http://premium.wpmudev.org/blog/wordpress-link-title-external-url/

查看更多
三岁会撩人
7楼-- · 2020-05-19 05:13

Use the "raw" plugin https://wordpress.org/plugins/raw-html/ Then it's as simple as:

[raw]
<script>
window.location = "http://www.site.com/new_location";
</script>
[/raw]
查看更多
登录 后发表回答