How To 301 Redirect Any Thesis Page

Thesis 1.7 has a sweet new post option that allows you to redirect any single post or page to any URL on the web. This is great for affiliate links along with a plethora of other uses. One problem. You can only redirect single posts and pages. What if want your archived (tag, author, date, category) pages to redirect somewhere? What if you want to redirect your 404 error page back to your homepage? I have a solution!

How 301 Redirects Work

A 301 redirect is basically just one line of code that tells a browser to go to a different URL than the one it is currently loading. Typically, you find these redirects at or near the top of the web page’s code. When the browser reads the code, it stops loading the page, and heads over to the URL its been redirected to. Simple! This is exactly what happens when you use the Thesis post option to redirect a single post or page to another URL.

The Code

This is really very simple. We’re going to use conditional tags, a native WordPress function, and a native WordPress hook. Theoretically, you could apply this exact same code to absolutely any WordPress theme and it should work! To demonstrate how this is done, let’s redirect your 404 error page to your homepage. Here’s the code:

function simple_redirect() {
	if(is_404()) { 
		wp_redirect( 'http://www.yoursite.com', 301 );
	}
}
add_action('template_redirect', 'simple_redirect');

The Breakdown

That’s all there is to it! Here’s what we did:

  1. We created a wordpress function called “simple_redirect” to house the code
  2. We use the conditional tag if(is_404()) to state that this redirect will only occur if this is the 404 error page
  3. We use wp_redirect to indicate the URL we are redirecting to, and to designate that this is a 301 redirect
  4. We use the WordPress hook template_redirect to hook our function

template_redirect places our function before any template (read: Thesis) files are loaded. This means our redirect won’t conflict with any of the header information that Thesis calls. All that’s left to do is copy and paste that code into custom_functions.php.

It really is that simple. Now you have total control over the route your visitors take when they come to your site!

Sign Up Now for Free Updates and Exclusive Content:

Learn how to write killer content, get more traffic, make money, and more by entering your email below:

Written by Adam Baird

Adam is a Wordpress designer, Thesis specialist, and blogger from Indianapolis, Indiana. He writes web design tutorials and resources on Theme Big. Check out his custom work here.

Comments