Turn a Wordpress Page into a Link
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.
Trackback





+1 for a WordPress tweaks section :)
You may want to escape the ‘o’ in your date format string. :)
Ahh, yeah, I thought I found it everywhere after the upgrade to php5… I wasn’t looking closely enough at the comments.
Cheers ;)
I implemented this tonight and love it. ;)
Glad to hear it!
Thanks a lot, this is incredibly helpful.
Mike
Saw this and thought of you. http://wordpress.org/extend/plugins/page-links-to/
Works like a charm! Thanks a heap.
Thanks a ton! After hours of looking, this tweak finally does what I wanted my site to do :)
Thanks man your my hero!
Its just plain stupid something like this is not in wordpress standard!
You could sell it and make millions.
haha, cheers guys, glad it’s helping.
Wow, this is exactly what I have been looking for! Thank you, you are awesome! Works perfectly!
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
<a_href=”/”>Home
About
whoops forgot to put spaces.
genius! wordpress should have it by default. smoochy thanks to you :)
Milorad you need to come back dude (as bleachy would say)
This is cool - but how do I get it to open in a new window instead of replacing the current one?
Cheers,
T
[...] http://www.ivovic.net/2008/05/20/turn-a-wordpress-page-into-a-link [...]
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]+)?)?)?@Wonderful! Thank you very much.
I came across this randomly and just wanted to say thanks for sharing this tip!
Justin
[...] 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 [...]
Hey,
My Name is, Paul
good overall content
look at my site:
http://HkFFjVzc.spaces.live.com/
Very informative and exactly what I was looking for. Worked perfectly. Thanks for posting this!
[...] 感谢Soundghost,让我写成这个帖子。感谢脚本原创者Milorad [...]
Thanks Man, this was really helpful.
In the article a huge thank you all for the cause, a lot of people are using
Thanks very much!
Saved some hours of my lifetime!
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
Thank you! A great solution to this problem.
(two_thumbs_up)
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.
Perfect!
Exactly what I was looking for!
Thanks!
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
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…
Genius, good , cool, what else can i say ?
thank you!
Many many many thanks!
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
Absolutely awesome post. I have been wondering how to do this for awhile now. Kudos for the easily implemented php redirect page.
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!
[...] 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 [...]
Thanks a lot! This is much better than flaky plugins which don’t always work!
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.
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.
Hey this is pretty awesome, just came across randomly and I found it, helped me a bit, after searching all over.
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
It really helped my site.
Thank you very much.
Muchas gracias, thank you and grazie - what a dead-simple way to get a link from my blog to my site!
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!!!
sorry… forgot to add
I’m running WP 2.7.1
nice! works great for me and fast switch 100 points for u :P
Its so tough to wotk
[...] Assign it a template type of Redirect [...]
4asc539ze7h1z8mm
如何把
Very Cool thanks You!
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….
Nice. I needed this big time!
[...] plugin de wordpress Page-links-to en lugar del script redirect.php (ahora hay que copiarlo por cada [...]
Thanks for this code, it was just what I was looking for.
[...] Ivović has an excellent post regarding Wordpress Pages - one that saved me plenty of frustration, and I hope you’ll find [...]
thanks so much! I couldn’t find this anywhere! <3
@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 !
Thanks for this tip. Will try using it today.
[...] like to thank the author of this blog for information on enabling WordPress “Pages” to redirect to a [...]
@Brian
use $pattern = ‘@(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@’;
and you should be able to use urls with hyphens
Excellent trick, and thanks…but how do I make it open the link in a new window?
Thank you very sir for this! It help a lot! cheers!
Thanks a lot. This is great solution.
Nice! Thank you so much, works like a CHARM
well.. it’s like I knew!
just wanted to say thanks for this. it was exactly what I needed.
Best,
Mike