How To Redirect a Page Using Custom Fields in WordPress

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.

You should see something like this:

custom-field

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.

15 Replies to “How To Redirect a Page Using Custom Fields in WordPress”

  1. Great. I’ll try your Custom Field Redirect plugin Nathan !

  2. pretty! going to try this on a current blog.

  3. If only I could shake your hand, or thank you in some way! This article is filled with one KILLER tip, it’s almost worth selling, and I’d be surprised if nobody agreed with me! – Just so smart, simple and easy to do, but with a big impact on what WordPress can do for you!

    While I’d personally use the code instead of the plugin, it’s just the type of person I am, and the less plugins I need to process a job the better! 😉

    Thanks
    Mark

    1. I prefer not to use plugins as well, so I know where you’re coming from. I certainly appreciate the support and enthusiasm. It gives me motivation to keep writing more articles and tutorials.

  4. […] How To Redirect a Page Using Custom Fields in WordPress 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. – By Nathan Rice […]

  5. […] How To Redirect a Page Using Custom Fields in WordPress — Nathan Rice 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. (tags: WordPress tips tutorial) […]

  6. I can see how this would be very useful in many cases. What I am curious about is how to redirect from the 404 page to an existing page depending on what URL was used to get to the 404 page. I ran into this problem when I switched my permalink from the ?### format to the /year/month/post-name format. Someone had linked to the ?### format link and that link generated a lot of traffic… so now all that traffic is hitting the 404 page right away! I am sure I could PHP up some static code on the 404 page to look at the URL and redirect from there… but I am sure there is a much better method for doing this. Heck, there may even be a built in WP function that does it that I don’t know about. 🙂

    1. I found a plug-in that handles the re-direct feature. I forget what it is called off the top of my head but easy to find with a search on the WordPress plug-in page.

  7. […] How To Redirect a Page Using Custom Fields in WordPress 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. – By Nathan Rice […]

  8. This works well but I need the URL it is redirected to to open in a new window. I can’t figure out what code to add to do that?

    Any suggestions are appreciated. Thanks!

  9. […] How To Redirect a Page Using Custom Fields in WordPress 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. – By Nathan Rice […]

  10. Nathan, I tried this with the code for my theme, and it failed to operate. Then I tried to install the plugin and it spit back a “This plugin does not have a valid header” error.

    Thoughts?

  11. Thanks for this awesome tip – I was looking for something that does just this. I also notice that it uses the 301 redirect as recommended by major search engines.

  12. I am also getting the:

    “This plugin does not have a valid header” error.

    It installs fine but once I try and activate it in my backend of WP it gives me that error.

    Help!

  13. Thanks Nathan, the re-direction worked perfectly, your guidance has been a great help, Alex.

Comments are closed.