Redirect a Page Using Custom Fields

Today, I wanted to share a very quick tip that will allow you to insert an internal or external URL in a custom field for a page, and when a user visits that page, they will be redirected to the URL you put in the custom field.

Why would you need to do this? Well, if you want to add a link to your navigation menu, instead of editing code, you could just create a new page, and have that page redirect your users to the URL you specified. I’ve had clients in that situation before, and this code has come in pretty handy for them.

Adding the URL to the Custom Field

The first thing you need to do is create a new page, or edit an old one. Scroll down past the page content to a section labeled Custom Fields.

If instead of seeing a text box on the left, you see a dropdown menu, just click the link that says Enter new and it will bring up the text box.

In that first text box (labeled Name), type in the word redirect, and in the second box (labeled Value, type the URL you want to redirect this page to. Then, click the Add Custom Field button.

Finally, either save or publish the page.

Editing Your Theme

After you’ve done this, you’ll need to add some code that will actually detect that you’ve created a custom field, and do the redirect. This will require a very minor edit to your theme, but don’t worry, it’s only 3 lines of code, and all you have to do is paste it in and save.

Open up your theme folder and look for a file called page.php. Open it up in your favorite text editor (not word processor) and look at the very top and locate this code (or something very similar):

<?php get_header(); ?>

All you need to do is change that, to this:

<?php
global $post; // < -- globalize, just in case
$field = get_post_meta($post->ID, 'redirect', true);
if($field) wp_redirect(clean_url($field), 301);
get_header();
?>

As you might have noticed, we’re using one of the 5 WordPress Functions You Didn’t Know Existed in this little code snippet, clean_url(). This just does some basic sanitation of the URL, just in case you pasted it in incorrectly. We’re also using the wp_redirect() function (source & Codex), which we didn’t cover in the 5 WordPress Functions You Didn’t Know Existed post, but it is definitely a good one. It takes a URL input, along with the type of redirect (in our case, a 301 permanent redirect), and does the redirect for us.

And that’s it. You’re done. Now, when a user visits your site and clicks on that page, they will be taken to the URL you specified in the custom field.

Isn’t There a Plugin for This?

Yes, and I wrote it. If you don’t want to mess around with editing code, you can completely skip that step, and instead, download and install my Custom Field Redirect WordPress plugin. Also, WordPress developer extraordinaire Mark Jaquith has a similar plugin called Page Links To. Either one will work fine for redirecting a page to a different URL.

Revisions

There are no revisions for this post.

Tags: ,

No comments yet.

Leave a Reply