Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Link to External Links from the Post Title in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Link to External Links from the Post Title in WordPress

Blogging is all about sharing resources, and often it is the case where blogger wants to just link to an external resource because he/she finds it useful to their readers. The main problem they face is that they have to make a new post in order to just tell the readers to go to another site. We have a solution for this problem and that is through a custom field hack which allows the WordPress blogger to link to the external resources in their post title. So when their user clicks on the post title, it takes them to another article.

This tutorial can help creating a resource section on your blog if you want to link to external resources, and it is a doorway to a lot more customization in WordPress if thought out correctly.

First thing you need to do is open your functions.php which is found in your template folder where other files like single.php and page.php is located. Paste the following code:

function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);

if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
} else {
$link = $perm;
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';

Now you would need to open your index.php and find the following code or something similar:

<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

Change it to:

<?php print_post_title() ?>

Once you have done that, upload both files to your webhost.

Now when you are writing a post, scroll down to where it says Custom Fields. Find the name: url1, title_url, or url_title and add the url to the external resource. Add a short description if you so desire, and hit publish.

Don’t be afraid, this function does not take away your normal post title links, all it does is add an extra query which checks for custom field for external links. If the external link is not included, it refers to the default code and link to the normal post page.

Update: We were informed by our user that there is a plugin for those users who don’t want to do the manual theme hacking by following this tutorial.

It is called Page Links To

Check it out and use that if you don’t want to deal with the codes.

This tutorial was requested by Tamar Weinberg who writes at Techipedia, and also work with Mashable Magazine. We encourage our users to ask us questions because their questions not only help them, but it helps the WordPress community as well.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. pamela says:

    i really needed to fix a link somewhere and i didnt know how to go about it, when i came across your article, i succeeded. Thanks

  2. WPBeginner Staff says:

    We would recommend you to use Page Links to plugin.

  3. Yao says:

    I can’t find <a href="” rel=”bookmark”> or something similar in my index.php. What is the other optin to put this ?

  4. WPBeginner Staff says:

    Yes but that’s not good. In the HTML output it will appear like this:

    External Link

  5. Ghap says:

    I found you can just put an anchor tag in your title and point the href to the external site or target. EX: POST TITLE

    Then once you click on title of blog it will redirect to external location.

  6. Sagar Patil says:

    these is not my first time on these site, i visited 2-3 months ago when i was started. and many of my problem was solved by reading your posts. now i wanted any plugin or coding for link my post directly to other website, i search and read many websites but finally got solution here, that Plugin. i used that its working. thank you because of your help and post i solved my problem again .

  7. Jong Fui Fa says:

    you solve my problem! Can’t thank you enough. Thank you and happy new year!

  8. Kohana says:

    Thanks for the tutorial, although I haven’t tried it out.

  9. Alexey says:

    Could anyone please help me with this: I want the title of some posts link to the external source (And I’m doing it with Page Links To plugin) while also preserving an internal permalink for my post so I could share it?
    You can take a look at sites like where many post titles link to external source but there’s a permalink icon (star) for the post itself to the right.
    Any ideas?

  10. florian says:


  11. igorlaszlo says:

    thanks for your post… i am not lazy at all :) but i prefer plugins not to have a mistake as i am not perfect in php coding… So, thanks to you that i found “page links to” plugin ! I just want to precise that the page you indicated in your article does not show anything anymore, people who want the plugin must look for it directly on WP plugins directory…
    The plugin lets posts & pages, both to be linked to anywhere you want and have an option if we want it to open in a new window or not… the options can be choosen at each post/page html editor below…

  12. Chazz says:

    This is a fantastic tutorial, thank you. Quick question, how does one make the external url open in a new window?

    • Chazz says:

      Without using a plugin I meant to ask (Plugins are the lazy way right?)

      • Editorial Staff says:

        No plugins are not the lazy way. We are using that plugin on this site. If you want to open without plugin method in a new window, then use target=”_blank” property in your anchor tag.

        • Chazz says:

          Where exactly is my anchor tag. I’m sorry if its a stupid question, I’m not a web designer. if you could just tell me where that is in the code. I’m sorry if I’m asking to many questions, and certainly don’t mean to offend with the “lazy plugin” joke.

        • Editorial Staff says:

          The anchor tag starts a href=”xyz”… add a target=”_blank”. You should use the plugin instead of hacking your theme. The good part about WordPress is that it has some very good plugins that are available.

  13. Patrix says:

    Does this no longer work with the latest version of WordPress (3.3.1)?

  14. Crysislevel says:

    nice tutorial! i didn’t know we could actually do this…now redirecting to another site is a lot easy. thanx! :) :P

  15. Felipe D. says:

    How can I get the same result in the site feed? Thank you!

    • Editorial Staff says:

      You would have to modify the feed code. Look at our article on how to manipulate RSS feeds.

  16. David says:

    Is it possible to style the external links so they are, say, a different colour compared to the standard post titles?

  17. Piet says:

    Hi I came across your article via a couple of links. What I am exactly looking for is somewhat different than what you have shown here, but I think it is a start.

    I don’t want to link to somewhere. I do however want to extract the title from the post_ID outside of the loop.

    Once I have extracted the title I can use that for a sidebar-function I am creating. Would part of your function suit that purpose?


    • Editorial Staff says:

      Yes it should work. You would have to use Query Posts function in the sidebar to get specific Posts with IDs…

  18. dejan says:

    Is there a “hack” to this hack :-) so external links open in new window?

    • Editorial Staff says:

      There are many plugins that will allow you to open external links in a new window. Install one of those.

  19. Simplywile says:

    It doesn’t work

    Parse error: syntax error, unexpected ‘>’ in /var/www/vhosts/ on line 317

    • Editorial Staff says:

      We have edited the code, if you directly copied and pasted the code, there were some formatting error in there.

      You can also use the plugin, it simplifies things, but the code should work as we have installed it in a few sites.

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 comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.