Redirecting a WordPress Post With Custom Fields
A couple of weeks ago I wrote a WordPress tutorial about Linking Post Titles to External Sites. I’ve received a few comments from readers on how to improve the WordPress code.
Technically, nobody clicking on a link on your website should be able to view the content (which most likely is blank). It is still possible for people to view the post if they either type in the URL or click an external link from an RSS feed or email.
To get around that, let’s make a small change to your header.php file which will force the users to be redirected, even if they’re accessing the page directly. Let’s add the following code at the very top of the PHP file:
<?php if (is_single()) {
global $post;
$thePostID = $post->ID;
$external = get_post_meta($thePostID, 'redirect', true);
if (strlen($external) != 0) {
header('location:'.$external);
}
}?>
Now, as with the earlier tutorial, if the post finds a value for the custom field redirect in the post, it will automatically redirect the user to the external URL.
You’ll see throughout my site I link to popular posts from fantastic authors such as James Altucher and the wonderful team at The Next Web. These posts are automatically linked to the source, saving me the time it would take to write long-winded, SEO-friendly articles to link back to what you, the reader really want – their great content.
With this simple piece of code, you’re taken directly from my website to theirs.

1 Comment
The Helpful Coder :: WordPress & Web Design Tutorials
[...] the answer Yes. Here’s why, WordPress can do almost anything. I wrote a while back about a simple redirect method for dealing with custom posts, but there is more than one way to skin a cat and while I follow the instructions in the earlier [...]
Leave a Comment