tags:

views:

240

answers:

6

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...

A: 

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.

BraedenP
Actually, you should be ok to add the header(); to a template if placed right after the php comment code used to name a page template file. Also, any code placed in the active themes function.php file should also execute before WP sends any headers.
Tim Schoffelman
Oh ok, perfect.
BraedenP
+3  A: 

You can accomplish this two ways, both of which need to be done through editing your template files.

The first one is just to add an html link to your navigation where ever you want it to show up.

The second (and my guess, the one you're looking for) is to create a new page template, which isn't too difficult if you have the ability to create a new .php file in your theme/template directory. Something like the below code should do:

<?php /*  
Template Name: Page Redirect
*/ 

header('Location: http://www.nameofnewsite.com');
exit();

?>

Where the template name is whatever you want to set it too and the url in the header function is the new url you want to direct a user to. After you modify the above code to meet your needs, save it in a php file in your active theme folder to the template name. So, if you leave the name of your template "Page Redirect" name the php file page-redirect.php.

After that's been saved, log into your WordPress backend, and create a new page. You can add a title and content to the body if you'd like, but the important thing to note is that on the right hand side, there should be a drop down option for you to choose which page template to use, with default showing first. In that drop down list, there should be the name of the new template file to use. Select the new template, publish the page, and you should be golden.

Also, you can do this dynamically as well by using the Custom Fields section below the body editor. If you're interested, let me know and I can paste the code for that guy in a new response.

Tim Schoffelman
+1  A: 

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.

Jason Leveille
Of course, that should be written as: header('Location: http://uri.com', true, 301);
Jason Leveille
Here is some more information regarding creating a custom WordPress template: http://www.davidrisley.com/2009/01/20/how-to-custom-page-template-wordpress/
Jason Leveille
A: 

There are also a few plugins that curl and cache Flickr accounts using the Flickr API. Note that if you implement either of the above, you may be violating their Terms of Service.

Bruce Alderson
A: 

This Codex FAQ will explain a simple method. Where it discusses Category URL, replace that with your URL.

How to easily display links to both Pages and Categories in the blog navigation header?

Michael
A: 

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.

Gipetto