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% [?]
































Twitter Comment
Create a WordPress Navigation Link to External URL | The Moxie Mom Blog [link to post]
– Posted using Chat Catcher
Thanks for the little trick. I tried it and it gave me this syntax error:
Parse error: syntax error, unexpected ‘@’ in /home/photosbymotti/www.photosbymotti.com/blog/wp-content/themes/infocus/redirect.php on line 7
I don’t have a clue how to fix php code.
Any solutions? Will be appreciatd
Thanks,
Motti