Turn a Wordpress Page into a Link

20th of May, 2008

I've been working with wordpress a lot lately though there's no real evidence of it on this site, unfortunately.  Nevertheless it's resulted in a few little 'tweaks' which I think may warrant their own little section here.  I'll think about that as keep collecting them.

In the mean time, I want to post something which I'm not using personally, but which provides a quick and creative (if a little dirty) solution to a theme-based problem that almost everyone who uses wordpress grapples with at least once.

A lot of the prefab themes out there use page-based navigation menus, but unfortunately this system doesn't allow you to insert custom links wherever you want them.  So what's the solution?

Well, using this little page template, you can turn any page into a link that leads anywhere you want it to.  Being careful to avoid any leading white-space, save the following code as 'redirect.php' and upload it to your theme's folder.

<?php
/*
Template Name: Redirect
*/

if (have_posts()) {
  the_post();
  $pattern = '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@';
  preg_match($pattern, get_the_excerpt(), $matches);
  if ($matches[0]) header('Location: '.$matches[0]);
  else echo 'Enter a URL into your page body text.';
}
?>

Now when you create a page, you'll see the option to use a template called 'Redirect'.  When using that template, your page can have any title you like, but the page content should be nothing but a link to your chosen destination.

Using wordpress' built-in page ordering mechanism, you should now be able to position that linky-page anywhere you feel it fits in your navigation menu.

This is one of the most common problems people have on the wordpress support forums, and it turns out the solution is actually quite achievable even for people who aren't confident enough to make changes to their theme's PHP templates.

There are of course better ways to build your menus, but if you're otherwise happy with your theme and all you want is a link or two, then here it is.

Fourteen Responses

  1. #1 20th of May, 2008 at 22:17

    +1 for a WordPress tweaks section :)

  2. #2 20th of May, 2008 at 22:22

    20th 2008f May, 2008

    You may want to escape the ‘o’ in your date format string. :)

  3. #3 20th of May, 2008 at 22:34

    Ahh, yeah, I thought I found it everywhere after the upgrade to php5… I wasn’t looking closely enough at the comments.

    Cheers ;)

  4. #4 22nd of May, 2008 at 16:22

    I implemented this tonight and love it.  ;)

  5. #5 22nd of May, 2008 at 18:42

    Glad to hear it!

  6. #6 28th of May, 2008 at 22:45

    Thanks a lot, this is incredibly helpful.

    Mike

  7. #7 2nd of June, 2008 at 19:24

    Saw this and thought of you. http://wordpress.org/extend/plugins/page-links-to/

  8. #8 8th of July, 2008 at 04:37

    Works like a charm! Thanks a heap.

  9. #9 19th of July, 2008 at 13:52

    Thanks a ton! After hours of looking, this tweak finally does what I wanted my site to do :)

  10. #10 24th of July, 2008 at 22:34

    Thanks man your my hero!
    Its just plain stupid something like this is not in wordpress standard!
    You could sell it and make millions.

  11. #11 25th of July, 2008 at 23:19

    haha, cheers guys, glad it’s helping.

  12. #12 30th of July, 2008 at 06:51

    Wow, this is exactly what I have been looking for! Thank you, you are awesome! Works perfectly!

  13. #13 27th of August, 2008 at 07:14

    Not sure if this works for every theme, but I was able to go in to the header file and add code so it looks like this:

    <a href=”/”>Home
    Link 1
    Link 2

  14. #14 27th of August, 2008 at 07:16

    <a_href=”/”>Home
    About

    whoops forgot to put spaces.

Respond Now

Comments are Gravatar enabled

Fields marked * are required.

Allowed XHTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Close
E-mail It