Hacking WordPress theme – external URL for post title

Sometimes something weird happens – I demand something that isn’t available. With software it’s easy (that’s how I got into computers), you dig in the code and you tweak and hack around until it does what you want.

When I just started this blog on Blogger quite a while ago, there was this feature that allowed to set up a link for your title – in case if you don’t want the title of the post to link to external resource. Why would you do it – whole another story, but it was just possible. With WordPress, even matured to version 2.3, it is still not.

I have decided to dig in and figure out how to do it with as least effort as possible. I have no idea how to build a WordPress plugin yet and I didn’t really have time to figure that out. So I decided to go hacking. Through various resources on the web I figured what needed to be hacked and how. Initial idea was to leave core files untouched and hack the theme only, so any time you upgrade your WordPress installation the feature remained untouched.

The only two places you will need to touch are functions.php and main index file of your template. This is such an easy process that almost any can do it in under 15 minutes.

In your WP admin open the Presentation -> Theme Editor screen and click on functions.php – you should see something like the following screen:
Hacking WordPress - click to enlarge

Open the attached hack file and copy everything from it into functions.php, then click Update File. When done, click on Main Index Template or on index.php – you should see something similar to the following screen:
Hacking WordPress - click to enlarge

Replace the line that says
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

with the following:
<?php print_post_title() ?>

Now, whenever you feel like let your post title leave your blog and point someplace else, just scroll down in your post writing screen, create or select a custom key named url1 or title_url or url_title and put that external URL in value box.

UPDATE: This still works for WP 2.5 and WP 2.6, read more in the update.

9 thoughts on “Hacking WordPress theme – external URL for post title”

  1. I have a couple of doubts about this hack.

    1) will the ext. URL show in feed?
    2)will it show in single pages?
    3will it show in other pages like archive/category/tag pages?

  2. Sumesh,
    as of right now the external URL shows up only in a list of posts. In all other circumstances it shows old permalink. I think I know how to fix it, so stay tuned.

  3. hey i have a proble after i put the link in custom field url1 this gets attached to the my blogs address..
    eg.if i write test.com in value fielt after publishing the post the title points to
    tech.passionvaibhav.com/www.test.com

    tell me what to do…

  4. Vaibhav,
    try to put http:// before the link, so your link would look as fully qualified URI.

    I will double check the code, maybe build it couple of regexps to check on validity of URL.

  5. Sumesh – If you modify single.php and archive.php in the same way as index, the external links will show instead of permalinks on those pages.

    I also add class=”‘.$pkey.'” into functions.php so I can easily style external links differently.

    For an example see:
    http://www.whatkatydid.me.uk/archives/105

    Hope this helps.

Comments are closed.