Create a WordPress Navigation Link to External URL
That is a wordy title! I had a terrible time figuring out a way to create a link to an external website. I found many websites with a lot of coding jargon. Here is a way to do it that is very simple. It took me about 2 minutes and it worked like a charm.
You are going to add a page to your WordPress blog BUT that page will only be a link to the outside URL of your choosing. It will magically be added to your header navigation and need no lengthy coding.
Most WordPress themes use page-based navigation menus, but unfortunately this doesn’t allow for you to insert custom links wherever you want them. So nothing’s perfect.
Let’s create a simple page template that will allow you to turn any page into a link that leads to whatever external URL you want it to go to. Make sure you don’t leave any white-space, save the code below as “redirect.php” and upload it to your theme’s folder (you should see another file called “single.php” and “page.php” in this same folder…among other things).
<?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.’;
}
?>
Go ahead and create a new page to take this new template for a spin. When you enter the title for the page it will be the title for your links navigation…same as always. Take a look at the “Attributes” and see there is now a Template called “Redirect.” There should be nothing on your page but the link to your chosen navigation menu: “http://www.smashingonlinesurveys.com” (whatever your URL is).
You’re done! Now that didn’t hurt did it?
Using WordPress’ built in page ordering function you can now position that external-page-link anywhere you want it in your navigation menu.
If you look on any WordPress support forum you will see this external page link issue is a common one. There are many ways, maybe a couple of plugins that can handle this common complaint but this is an easy fix using this .php file.
There are other better ways to build your menus, but if you’re like me you love your theme and all you want is to add an extra link or two.
See, I told you it was easy
If I can do it! You can do it too! Celebrating Texas country living, down home cooking, family life, figuring stuff out and being a stay-at-home moxie mom computer geek.
Blessings,
Wendy Merritt
The Moxie Mom
XXO (A kiss on each cheek and a hug)
If you enjoyed this article get updates via my RSS feed.

Popularity: 32% [?]





























Thanks for the tip! One thing, though: When I cut and paste straight from your article it copied into my text editor with smart single quotes, which led to a parsing error. Just might want to give a heads up on that one!
Once I cleaned out the smart quotes for non-smart quotes, everything worked like a charm!
Hi,
Thanks your article is really clear and easy to follow. I did everything you listed however I get this error.
Parse error: syntax error, unexpected T_IF in /home/liz49935/public_html/journal/wp-content/themes/tofurious/redirect.php on line 1
I have no programming skills and I am customising my wordpress blog through articles/forums. So I don’t know what could be wrong.
Any ideas?
Thanks
Wendy Merritt
Reply:
February 20th, 2009 at 10:14 am
Lizzy…I am not a programmer either…I just know enough to get myself into trouble…LOL Let me see if I can find a solution for you. What version of WordPress are you running? What theme are you using? It could just be a conflict with these.
Do any readers have any solutions for the lovely Lizzy?
Lizzy
Reply:
February 20th, 2009 at 9:10 pm
thanks Wendy.
I am using wordpress 2.7.1 and my theme is one I got from the web (tofurious)
cheers,
Lizzy
zucchini
Reply:
February 22nd, 2009 at 2:47 pm
No solution…
just the same problem
Parse error: syntax error, unexpected ‘@’ in /home/mhd-01/www.dgclick.com/htdocs/dg/wp-content/themes/sleek/redirect.php on line 8
hello again!
So I found an other solution for our problem!
Sorry Wendy,
but instead of using the redirect.php I just inserted in my header.php
the following:
EXTERNALLink
1) find the menu code which looks somethink like this
(depending on theme of course)
2)copy the line from above and fill in your external link, and the name of the tab which should link to the external site!
That’s it!
zucchini
Reply:
February 22nd, 2009 at 4:01 pm
Well check it out here..better explained
http://www.okaay.com/wordpress-how-to-create-external-links-in-navigation-menu/
Wendy Merritt
Reply:
February 26th, 2009 at 6:14 pm
Another nice explanation for creating an external link on WordPress…thanks for the great link
Many things with your WordPress blog are going to hinge on what your current theme is, what browser are you using, Windows vs. Mac and so on. This is another great alternative and appears to have worked for many. Thanks again, Zucchini.
Sounds just the solution I am looking for, but I am really veeeeeery new to this blogging business.
So a dumb question I know, but where do I copy this code to and how do I save it as a “redirect.php” file?
Please help if you can
regards
Rob
Wendy Merritt
Reply:
April 28th, 2009 at 12:51 pm
Rob, the only kind of dumb question is the one not asked. Thank you for asking a great question. I probably should have elaborated a little better in my instructions.
1. Highlight the text/code you are trying to save as the “redirect.php” file
2. Right click on the highlighted text and click on COPY
3. Open a text editor program like NOTEPAD
4. Right click and click on PASTE
5. Click on FILE
6. Click on SAVE AS…
7. browse on you pc/laptop where you would like to store this file
7. Enter the file name, in this case enter REDIRECT.PHP (FYI – Save as type: will probably say “Text Documents (*.txt) and Encoding: ANSI)
Now your file is saved as a .php file. I hope this clarifies this process for you. If you still have questions don’t hesitate to leave another comment. Enjoy your new blogging adventure.
Blessings,
Wendy
P.S. If you have a topic you would like me to cover for you just browse to the Contact Me page and send me an email. I’d be happy to help if I can.
This redirect works great, but what if I need to implement it into a template for pages that require login before the redirect happens?Using:
to handle logins. Thanks for any advice
Madeline
Reply:
July 2nd, 2009 at 11:56 am
Sorry — should have stripped the php tags — using if (is_user_logged_in()) to handle logins.
Wendy Merritt
Reply:
July 6th, 2009 at 6:30 pm
Madeline, Glad you got it all figured out. I’d love to see your site. I didn’t see a link attached to your comment.
Blessings,
Wendy
I remember this code from about 2 years ago. I say that if you are going to use someone’s code that you credit them for their work. I would be hard pressed to believe that two people developed the same plugin within a years past.
Please give credit where credit is due when you write these plugin posts. Thank you.
Sam Diamond´s last blog ..Bill Burr’s Performance on the New Conan O’Brien
Wendy Merritt
Reply:
July 6th, 2009 at 6:23 pm
Sam, I appreciate your “credit” concern but I would hardly call this a rocket science tweak. This is not a plugin but a work around in the coding of WordPress. It is merely a way to get WordPress to do something it doesn’t want to do…kind of like taking a horse to water and actually getting him to drink it. I am sure there are many other people who have created similar code tweaks to this one.
You are correct, however, in the fact that if you are quoting, lightly editing or continuing the conversation from someone else’s information you should give them credit. Thanks for giving my readers a heads up.
Blessings,
Wendy
Sam Diamond
Reply:
July 8th, 2009 at 12:09 pm
I have the exact same code. Not one piece of the code is different. I can reference the page here if you want. I am only saying that I have used this code for nearly two years, and your code is exactly the same.
I will send it to you if you want, or you can see it in action on one of my clients website. I am only saying, I find it hard to believe that you named it exactly like others have named it, and you even placed specific selectors in the code ( because I can tell you from experience, some themes screw up the permalinks, which is why I created a better code a few months back ).
I am only saying, if you made this code, then you have made an exact copy of someone else’s. I find that absolutely amazing, conceding you never came across this code before.
Sam Diamond´s last blog ..David Wright Looks Like Chi-Chi from Daisy of Love
Sam Diamond
Reply:
July 8th, 2009 at 12:11 pm
http://www.ivovic.net/2008/05/20/turn-a-wordpress-page-into-a-link
Here is the original code. Can you explain how both of you developed the exact same code with the exact same name? And if not, could you please reference the author of the code in your post?
Thank you.
Sam Diamond´s last blog ..David Wright Looks Like Chi-Chi from Daisy of Love
Wendy Merritt
Reply:
July 8th, 2009 at 2:31 pm
Sam, I have never been to the site you have referenced much less copied his code. Not to mention he really offers no directions but you will notice mine has step by step instructions. There are only 13 lines of code here. It wasn’t a copy. As I have said there are many people who have generated similar codes to do the exact same thing. I do not have time to research them all and see how similar they all are to each other. But, hey, knock yourself out.
Blessings,
Wendy
The reason I bring this up is because the code doesn’t work well with permalinks within certain structures, or with certain affiliate link codes.
I was asked by a client to tweak it and I have a better version. But the current one you have has problems with specific themes ( Pressbox, Classipress ) so if you need a fix I can give it to you.
Sam Diamond´s last blog ..David Wright Looks Like Chi-Chi from Daisy of Love
Wendy Merritt
Reply:
July 8th, 2009 at 2:37 pm
I have never been to the website you have referenced in your previous comment. Yes, his post was made before mine. Yes, his code does the same thing as mine. Sure, I’ll be happy to reference someone who has created a similar code to mine…oh, you have already done that for me.
I have had no issues with permalink errors or conflicts with my code. If you want to make a post with your update on your blog and send a backlink to this post…have at it. I am a hack (I live with my PHP for Dummies book right beside my pc) and not a coder so I don’t have any current intentions for updating this post.
This is now a dead issue. I hope you have enjoyed all the link love and traffic you have received from my site.
Blessings,
Wendy
Sam Diamond
Reply:
July 8th, 2009 at 3:30 pm
You are more than welcome to take my links off ( you use wordpress and you can edit them out ). I received one person ( I guess it was you ) to my site. I don’t think there is anyone from marketing blog that strays to a sports site from a comment.
It is a dead issue. Take care.
Wow, thanks. That really came in handy!
Wendy Merritt
Reply:
August 6th, 2009 at 12:38 am
Happy to have helped you.
Blessings,
Wendy