X

How to Redirect to URL from Title in WordPress

Snippets by IsItWP

Are you looking for a way to redirect to URL from title? While there’s probably a plugin for this, we have created a quick code snippet that you can use to redirect to URL from title in WordPress.

Imagine that you run an online-magazine. At some time you may need (or want) to redirect your readers to another website right from your homepage without having them to read trough your post and click the link there. You could use a plugin OR you simply use custom field magic.

Now all you need to do is to add a custom field called “redirect” (this is case-sensitive) with your desired URL as value. When your readers click on a certain title on your homepage, they will be redirected to the other website.

Instructions:

All you have to do is add this code to your theme’s single.php file:


// Search for something that looks similar to this in your index.php
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>

 // and replace it with

<a href="<?php $redirect = get_post_meta($post->ID, 'redirect', true);
if ($redirect != "") { echo $redirect; } else { the_permalink(); } ?>" rel="bookmark">
<?php the_title(); ?></a>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress themes for travel blogs and how to create a guest post submission form in WordPress.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!