Make a WordPress Title Link to an External URL

Posted on

When you view a list of your posts either on the WordPress homepage or in a category/date archive, the post titles link to the URL associated with that specific post. This way, you can show a small excerpt for example and entice people to read the complete article clicking on the link. WordPress uses a set number of functions to achieve this effect. But what if it’s not the behavior you’re looking for? Suppose your site specializes in redirecting people to external resources and you don’t want your visitors to have to click through to the single post before they can obtain the URL?

In such a situation, your standalone post is not going to provide much additional value add to a visitor. All crucial information is already presented in the title. What do you do then? By default, WordPress doesn’t allow you to change the link associated with the title. However with some clever filtering, we can use the tried and tested method of custom variables to bend WordPress to our will. Let’s take a look at how it’s done.

Creating a New Custom Variable

Let’s say you’re creating a new post where you want the title to link to another URL. Open it up in the default WordPress editor and scroll all the way down past the editing box till you come to the section labeled “Add New Custom Field”. To add a new variable, click the “Enter New” link. This will present you with two text boxes – one on the left and one on the right. In the one on the left, enter the variable name:


In the text box on the right, enter the destination URL that you want your post to link to. When you’re done, it should look something like this:

custom variable with linked URL

Click the “Add Custom Field” button to add it to the list of existing custom variables and save your post. Once you save this information, it’s time to add some additional code to WordPress.

Using the Variable in WordPress

Even though we’ve created a custom variable, it means nothing until we actually instruct WordPress to use it appropriately. To do this, open up your functions.php file and paste the following before the closing ?> PHP tag:

function link_to_another($url) {
	if(get_post_meta(get_the_ID(), 'link_to_url', true) && !is_admin())
		return get_post_meta(get_the_ID(), 'link_to_url', true);
		return $url;
add_filter('post_link', 'link_to_another');

If you don’t know how to add custom code to WordPress like this, take a look at my earlier tutorial on the same. The code above hooks into the “post_link” filter which is used as a “behind the scenes” tool to generate the URL of posts and pages. What we’re doing here is simple – we check to see whether or not our entry has a custom variable and if so, uses that instead of the original. In the absence of such a custom variable, we only have to return the initial URL that was passed. Save your functions.php file and open up your blog to view your page or post when it’s part of an archive. The homepage will do just fine.

If all has gone well, you should be able to see that your post now links to the custom variable URL that you specified in the very first step as shown here:

URL changed

All the other blog entries will be unaffected if they don’t have a special “link_to_url” custom variable. This is a scalable and unobtrusive way to link WordPress titles to external URLs that should work with every theme and don’t require you to modify any existing theme code.

One Reply to “Make a WordPress Title Link to an External URL”!

Leave a Reply

Your email address will not be published. Required fields are marked *