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.

Seventy-one 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!

  29. #29 29th of November, 2008 at 20:45

    Hi there, I’m using this template, be everytime I click the link I get the following error message:

    Warning: Cannot modify header information - headers already sent by (output started at /websites/LinuxPackage02/ey/et/ho/eyethomson.com/public_html/blog/wp-content/themes/black-n-white/redirect.php:9) in /websites/LinuxPackage02/ey/et/ho/eyethomson.com/public_html/blog/wp-content/themes/black-n-white/redirect.php on line 18

    Cheers

  30. #30 29th of November, 2008 at 22:31

    Thank you! A great solution to this problem.
    (two_thumbs_up)

  31. #31 1st of December, 2008 at 13:03

    Hey man thanks for code, it works great! I’m totally new to WordPress and I’m trying to tweak a template for a blog that links to our chainsaw EPN site.

  32. #32 2nd of December, 2008 at 04:07

    Perfect!
    Exactly what I was looking for!
    Thanks!

  33. #33 7th of December, 2008 at 21:24

    Hi, I salivated when I saw your post. However, using WP 2.7 RC1, it is not working for me.
    Have you heard any other stories like this?
    Or is there one common problem that might be recurring here?
    Many thanks, Steve

  34. #34 2nd of January, 2009 at 10:59

    freakin’ sweet!  This was the last step to a WP overhaul I was working on today…figured it would be much hard to do this until I found your post.  Thanks for the hack and sharing!

    FYI: works fine under WP 2.7…

  35. #35 8th of January, 2009 at 01:09

    Genius, good , cool, what else can i say ?

  36. #36 14th of January, 2009 at 02:22

    thank you!

  37. #37 15th of January, 2009 at 20:09

    Many many many thanks!

  38. #38 28th of January, 2009 at 21:01

    It not to late to say thank you… I will try this tip.
    Because :
    1. I try by cpanel with “redirect” menu .. I do not know, why its not working… (by wild card and not using wild card too)
    2. try using permalink … ha ha ha … it just fix strange/freak link.

    Oke thank you

    http://www. gastia.com

  39. #39 29th of January, 2009 at 22:28

    Absolutely awesome post.  I have been wondering how to do this for awhile now.  Kudos for the easily implemented php redirect page.

  40. #40 3rd of February, 2009 at 04:23

    Thanks for the excellent tweak! I got it all working within minutes.

    I do have the same question as someone else… wondering if there is a way to do this as a target_blank?

    Thanks!

  41. #41 15th of February, 2009 at 01:03

    [...] pages, but I did that just because like I said, I’m anal. I also added a .php page for an automatic redirect from a page (using a page as a link) so I could link to my personal web [...]

  42. #42 15th of February, 2009 at 16:52

    Thanks a lot! This is much better than flaky plugins which don’t always work!

  43. #43 18th of February, 2009 at 17:45

    It’s not working for me. I don’t know if the fact I haven’t changed the domain’s DNS to the proper nameservers that could be affecting it. Or a bug with Wordpress 2.7.

  44. #44 19th of February, 2009 at 03:28

    TOTAL BLING!

    Current dev is only my 2nd WP site. I didn’t want to implement a heavy plugin (with conflicts) for my client’s rigid ideas of navigation but rather wanted to use my very new PHP skills. I wasn’t even thinking redirect. So simple when I see it in hindsight I just want to say, “Doh!”

    Great idea and thanks a million for sharing it.

  45. #45 19th of February, 2009 at 08:32

    Hey this is pretty awesome, just came across randomly and I found it, helped me a bit, after searching all over.

  46. #46 10th of March, 2009 at 16:31

    Hey thanks for the great help.

    Just thought you should know moxiemom has copied your article and code almost verbatim - no reference to you.

    http://www.themoxiemomblog.com/wordpress/create-a-wordpress-navigation-link-to-external-url - posted 16-1-2009

    Also her code did not work, directly by copy - paste, may have been one of the issues pointed out and resolved above.

    Anyway thought you should get the cred for the good work.

    “Give credit where credit is due”

    Thanks

  47. #47 11th of March, 2009 at 02:13

    It really helped my site.

    Thank you very much.

  48. #48 11th of March, 2009 at 15:57

    Muchas gracias, thank you and grazie - what a dead-simple way to get a link from my blog to my site!

  49. #49 21st of March, 2009 at 01:09

    When i add a url with a hyphen(-)

    example:

    http:www.domainname.com/wp-admin

    It does not work. It does not see the hyphen and gives a page not found….

    It works great when I point to URL’s with no hyphen.

    Any work around to this? Also…. Anywhere i can donate some $$$ for your time and this. It’s something clean and simple!!! Great tool!!!

  50. #50 21st of March, 2009 at 01:10

    sorry… forgot to add

    I’m running WP 2.7.1

  51. #51 15th of April, 2009 at 07:40

    nice! works great for me and fast switch 100 points for u :P

  52. #52 18th of April, 2009 at 01:18

    Its so tough to wotk

  53. #53 25th of April, 2009 at 04:06

    [...] Assign it a template type of Redirect [...]

  54. #54 1st of May, 2009 at 06:22

    4asc539ze7h1z8mm

    如何把

  55. #55 5th of May, 2009 at 00:30

    Very Cool thanks You!

  56. #56 12th of May, 2009 at 04:39

    THANK YOU! I’m in the process of taking a design and converting it to wordpress/css. This solved one of the bigger “dunno how i’m gonna handle THAT” coding issues that the site had….

  57. #57 22nd of May, 2009 at 12:34

    Nice. I needed this big time!

  58. #58 1st of June, 2009 at 09:58

    [...] plugin de wordpress Page-links-to en lugar del script redirect.php (ahora hay que copiarlo por cada [...]

  59. #59 7th of June, 2009 at 02:52

    Thanks for this code, it was just what I was looking for.

  60. #60 8th of June, 2009 at 09:07

    [...] Ivović has an excellent post regarding Wordpress Pages - one that saved me plenty of frustration, and I hope you’ll find [...]

  61. #61 11th of June, 2009 at 16:35

    thanks so much!  I couldn’t find this anywhere!  <3

  62. #62 13th of June, 2009 at 01:05

    @OP : If you were a girl , then i would have kissed you for this , this trick was awesome , i tried many tricks , this one , worked awesomely .

    *muah* for you :D

    And Thnx a lot !

  63. #63 13th of June, 2009 at 10:44

    Thanks for this tip. Will try using it today.

  64. #64 14th of June, 2009 at 05:11

    [...] like to thank the author of this blog for information on enabling WordPress “Pages” to redirect to a [...]

  65. #65 17th of June, 2009 at 02:32

    @Brian
    use $pattern = ‘@(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@’;
    and you should be able to use urls with hyphens

  66. #66 18th of June, 2009 at 00:35

    Excellent trick, and thanks…but how do I make it open the link in a new window?

  67. #67 21st of June, 2009 at 15:58

    Thank you very sir for this! It help a lot! cheers!

  68. #68 21st of June, 2009 at 18:01

    Thanks a lot. This is great solution.

  69. #69 27th of June, 2009 at 15:26

    Nice! Thank you so much, works like a CHARM

  70. #70 29th of June, 2009 at 03:03

    well.. it’s like I knew!

  71. #71 3rd of July, 2009 at 19:51

    just wanted to say thanks for this. it was exactly what I needed.

    Best,
    Mike

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