How To 301 Redirect Any Thesis Page

Summary

You Will Use:

PHP

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!

Take Action Now!

  1. Choose the proper conditional tag for the page you wish to redirect
  2. Be sure to enter the correct URL you are redirecting to
  3. Copy and paste the code into custom_functions.php in your Thesis "custom" folder

Make More Money From Your Blog

Insider Updates

Learn how to make more money with actionable no-nonsense marketing tips. Sign-up below and immediately receive our 5 part blog marketing crash course.

Written by

(photo by penywise)

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.

Adam has written 16 posts for Art of Blog!

{ 2 comments… read them below or add one }

wedding May 30, 2010

thank you ———- ive been searching like crazzy 4 this code.
and works ok
Your the man!!!!!!!!

Reply

Nisha September 1, 2010

Thank for the code

Reply

Leave a Comment

{ 2 trackbacks }