Back to Top

Redirect external links in WordPress

Previous Post:

Redirect external links in WordPress

Whether you’re a WordPress user or not, sometimes it’s important to redirect external links on your website via a redirection page (or a disclaimer page), this is especially true on sites that have user-contributed contents or lots of images/download links.

With the help of BWP External Links (a WordPress plugin that helps you easily control links to external domains), and some custom codes, you can build a nice redirection page in no time. Note that some parts of this guide can be applied to other platforms as well and not just WordPress.

The first step is to create a redirection page and then we will configure BWP External Links to use that page as a prefix to all external links.

There are more than three ways to create a redirection page (e.g. a real WordPress page or a fake WordPress page), but for the sake of simplicity we will use a real page. So create one and name it the way you like, but take note of its permalink. For this guide I will simply use “Redirect To” as the redirection page (permalink is redirect-to).

Now, it’s time to build the contents of the page. The most straightforward approach is to create a custom theme file, i.e. page-redirect-to.php and put it into your theme’s folder. If you visit http://example.com/redirect-to/ now you should be greeted with an empty page (which is correct). Note that it’s also possible to use a Page Template but it’s actually the same thing.

A redirection page should have at least two components: a disclaimer text that warns about external links and a button or an auto-refresh script that takes care of the actual redirection. Oh, and such page should have the same look and feel as any other pages on your website. To achieve that, use this redirection page skeleton:

<?php
// get the redirection url from GET variable
$redirect_to = !empty($_GET['url'])
	? trim(strip_tags(stripslashes($_GET['url'])))
	: '';

$wait_time    = 5000; // time to wait before redirection occurs, in milliseconds
$wait_seconds = $wait_time / 1000;

add_action('wp_head', 'redirect_to_no_index', 99);
function redirect_to_no_index()
{
	// this page should not be indexed at all
?>
	<meta name="robots" content="noindex, follow">
<?php
}

add_action('wp_head', 'redirect_to_external_link');
function redirect_to_external_link()
{
	global $redirect_to, $wait_seconds, $wait_time;

	if (empty($redirect_to) || empty($wait_time)) {
		return;
	}

?>
	<script>var redirect = window.setTimeout(function(){window.location.href='<?php esc_html_e($redirect_to); ?>'},<?php echo $wait_time; ?>);</script>
	<noscript><meta http-equiv="refresh" content="<?php echo $wait_seconds; ?>;url=<?php esc_attr_e($redirect_to); ?>">></noscript>
<?php
}

get_header();
?>
	<div id="page-content">
		<div class="redirect-message">
			<?php if (!empty($redirect_to)) {
					printf(__('You are about to leave this site and will be automatically redirected to <strong>%s</strong> in %d seconds.'),
						esc_html($redirect_to), $wait_seconds);
				} else {
					_e('Invalid url to redirect to');
				}
			?>
		</div>
	</div>
<?php
get_footer();

The above skeleton should take care of two additional things: 1) instruct robots to NOT index the page but DO follow links on it, and 2) check for empty redirection urls, i.e. if you simply visit http://example.com/redirect-to/ a message stating that the url is invalid will be shown and no redirection would occur.

You can change $wait_time to any number you like (default is 5000 milliseconds, which is the same wait time used on this website), but make sure that it’s in milliseconds (a thousandth of a second).

If you prefer buttons instead of auto-redirection, change $wait_time to 0 and then replace codes from line 38 to 41 of the above skeleton with below codes:

<?php if (!empty($redirect_to)) {
	_e('You are about to leave this site and follow a link to an external site, '
	. 'please confirm this action or go back to previous page.'); ?>
	<br />
	<button onclick="window.location.href='<?php esc_html_e($redirect_to); ?>';return false;"><?php _e('Confirm'); ?></button>
	<button onclick="history.go(-1); return false;"><?php _e('Go back'); ?></button>
<?php } else {

Try refreshing the redirection page and you should see two buttons, one allows you to continue with redirection, while one allows you to go back to previous page. Cool, isn’t it?

Now that we have a working redirection page, the remaining task is easy. First, make sure that BWP External Links is up and running (a small icon should appear next to an external link). Next, set a custom prefix for external links: navigate to Settings > BWP External Links > Links Settings > External link prefix, select “A custom URL”, and then type in the custom prefix you want. For this guide it should be http://example.com/redirect-to/?url=.

If you change ?url to something else (e.g. ?link), make sure you change $_GET['url'] to $_GET['link'] as well on line 3 and 4 of the skeleton.

That’s it, give yourself a pad on the back for creating such a cool redirection page for external links on your site :-).

Limitations: You might not have noticed, but there are two limitations with this approach: 1) you can’t set a response header for the redirection page (it should be 302 or 303), and 2) a trailing-slash is often added to the end of the page, i.e. http://example.com/redirect-to/?url= (notice the slash just before ?url? It’s not a big deal, but kind of annoying). There’s workaround for these limitations, but let’s save it for another tip, shall we?

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

23 Opinions for Redirect external links in WordPress (18 Trackbacks)

  1. User's Gravatar
    1
    Nouman February 28, 2016 at 12:04 am – Permalink

    Thanks Bro Great Tutorial …Working For Me ?

  2. User's Gravatar
    2
    Max May 19, 2016 at 1:55 pm – Permalink

    Hello, this does not work, you can help me?

    I created a directory in the root of the site – redirect-to
    Then put back the file page-redirect-to.php, it put the code from this page
    When you click on a link error occurs, see
    http://udarmail.ru/redirect-to/page-redirect-to.php?url=http://betterwp.net/wordpress-tips/redirect-external-links/

    A page with a redirect http://udarmail.ru/redirect/

    I’ll be waiting for your response, and sorry for my english.

  3. User's Gravatar
    3
    Viral Story August 9, 2016 at 10:34 pm – Permalink

    It really help me lot in creating external links as nofollow and monetise link shortner.

    Thanks.

  4. User's Gravatar
    4
    steve August 26, 2016 at 11:02 pm – Permalink

    I’m having trouble when the new ridirect page appears. It just says “No content in this loop” then it does not redirect.

    Any help??

    Thanks,

  5. User's Gravatar
    5
    Kenh76.vn September 25, 2016 at 2:39 am – Permalink

    Hi,
    First I would like to thank for your very useful plugin.
    But there are problems plugin: https protocol, Redirect external linksdo not work.
    How to solve?
    Thank you.

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.