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.

Twenty-eight 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.

  15. #15 31st of August, 2008 at 04:01

    genius! wordpress should have it by default. smoochy thanks to you :)

  16. #16 9th of September, 2008 at 16:30

    Milorad you need to come back dude (as bleachy would say)

  17. #17 18th of September, 2008 at 00:09

    This is cool - but how do I get it to open in a new window instead of replacing the current one?

    Cheers,

    T

  18. #18 24th of September, 2008 at 03:51

    [...] http://www.ivovic.net/2008/05/20/turn-a-wordpress-page-into-a-link [...]

  19. #19 24th of September, 2008 at 16:55

    Thanks for this very useful tip.
    If you have ‘-’ in your URL then this will clip the URL so the RegEx should be: @https?://[-.\w]++(:[\d]+)?(/([-/_.\w]*(\?[\S]+)?)?)?@

  20. #20 10th of October, 2008 at 01:23

    Wonderful! Thank you very much.

  21. #21 10th of October, 2008 at 08:01

    I came across this randomly and just wanted to say thanks for sharing this tip!

    Justin

  22. #22 18th of October, 2008 at 01:18

    [...] you want to have a Wordpress Page link to an arbitrary url try this.  It tells you how to create a Page template called redirect.php with the [...]

  23. #23 21st of October, 2008 at 14:16

    Hey,
    My Name is, Paul
    good overall content
    look at my site:

    http://HkFFjVzc.spaces.live.com/

  24. #24 26th of October, 2008 at 14:22

    Very informative and exactly what I was looking for.  Worked perfectly.  Thanks for posting this!

  25. #25 28th of October, 2008 at 00:12

    [...] 感谢Soundghost,让我写成这个帖子。感谢脚本原创者Milorad [...]

  26. #26 28th of October, 2008 at 20:36

    Thanks Man, this was really helpful.

  27. #27 6th of November, 2008 at 12:04

    In the article a huge thank you all for the cause, a lot of people are using

  28. #28 15th of November, 2008 at 03:35

    Thanks very much!
    Saved some hours of my lifetime!

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