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.