Creating a page redirect with WordPress

Posted on July 18th, 2009 by Dave in Blog, Wordpress

wordpress-250x250If you use WordPress Pages to manage the content of your site, sometimes you may want a page to actually redirect to another URL – either an external site, or perhaps a subdomain of your existing site. Or maybe you want to redirecting to a static file, such as a PDF.

There are a number of ways this can be done, most of which require adding a plugin to your site. My personal philosophy is that there are a lot of great, feature rich plugins out there, however a lot can be accomplished with just a few lines of simple PHP code. I only use plugins when I feel there is significant benefit.

By creating a simple page template and combining that with a WordPress Custom Field, I can have a flexible and easy to use redirect method.

Step 1: Create the page template, and save the new page in your theme folder as ‘redirect.php’.

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

<?php
global $post;
$field = get_post_meta($post->ID, 'redirect', true);
if($field) wp_redirect(clean_url($field), 301);
?>

Step 2: Create a new page

  • Create a new page, give it a title, and choose the “Redirect Page” page template from the right hand menu.
  • In the Custom Fields section, create a field with a Name of “redirect”. In the Value field, enter the absolute URL to the location where you want to redirect (eg http://somesite.net/somefile.htm).
  • Publish the page

Voila, you have a reusable redirect technique for your WordPress site.

  • 0 Comment
  • Tags:

What do you think? Join the discussion...

How do I change my avatar?

Go to gravatar.com and upload your preferred avatar.

Archives

Tag Cloud

BuddyPress business Conference Install Twitter WampServer WordCamp Wordpress WordPress MU