WordPress Page Redirect

There are a variety of ways to achieve a dynamic page redirect from within your WordPress theme or plugin.

If you are only interested in static redirects (i.e. always redirect one page address to another), then it is most efficient to use 301 redirects directly from your web server.

Unlike static redirects, dynamic WordPress redirects will allow you to load pages based on user input or WordPress blog state.

1. WordPress wp_redirect Command

The WordPress wp_redirect command allows you to redirect the web browser onto a page or location of your choice from within your plugin or theme files.

Of even greater use is the wp_redirect filter. With this filter, you may alter the locations set by core WordPress functions or by other themes and plugins.

For example, in my Media Library plugin, I alter the redirect location set within the core WordPress Media Admin Panel (wp-admin/upload.php).

// Change redirect set in upload.php
function media_plus_redirect($location, $status) {
	if ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
		if (!isset($_GET['detached']))
			$location = remove_query_arg('detached', $location);
	}
	return $location;
}
add_filter('wp_redirect', 'media_plus_redirect', 10, 2);

In the add_filter function

  • The 2nd argument is your filter function name.
  • The 3rd argument is your function priority.
  • The 4th argument is the number of arguments accepted by your filter function. The wp_redirect filter function accepts 2 arguments.
function media_plus_redirect($location, $status) {
   // Your redirect function code here
   return $location;
}

If you are doing page redirects within a WordPress theme or plugin, it is best to use the wp_redirect command. This allows others to extend or customize your plugin/theme more easily through the wp_redirect filter hook.

2. PHP Redirects – Sending Header Information

Instead of using the wp_redirect command, you can also use PHP to dynamically redirect the browser by sending appropriate header information.

Note - This will only work if the PHP redirect is issued before the page header is defined. Otherwise, an error will be triggered because you are trying to define multiple headers in a single web-page.

<?php header( 'Location: http://www.shibashake.com' ) ; ?>

The wp_redirect command uses a PHP redirect at its core, therefore, it will not work either after header information has been defined for a page.

3. Javascript Redirects – For Redirect Operations After Header Definitions

Sometimes, it may be necessary to issue a page redirect after headers have been defined. This may occur when you need to do page redirects from within a WordPress hook function.

The function may be tied to a hook that is only called after header definitions.

In this case, we can use Javascript redirects.

// redirect after header definitions - cannot use wp_redirect($location);
?>
   <script type="text/javascript">
   <!--
      window.location= <?php echo "'" . $location . "'"; ?>;
   //-->
   </script>
<?php

And there you have it – have fun with re-directions!

Related Articles

Comments

  1. Chris says

    Hi,

    i tried to redirect a category page but somehow it doesnt work.
    i put this code in the functions.php (theme dir)

    function _portfolio_plus_redirect( $location, $status )
    {
    if ( isset($_REQUEST['category_name']) && $_REQUEST['category_name'] == ‘portfolio’ )
    {
    $location = remove_query_arg( ‘category_name’, $location );
    $location = add_query_arg( array( ‘page’ => ”, ‘pagename’ => ‘portfolio’ ), $location );
    }

    return $location;
    }

    add_action( ‘wp_redirect’, ‘_portfolio_plus_redirect’ );

  2. says

    I tried to do 301′s in a more hardcoded kind of style, but using the filter approach definately looks like the best approach to do 301′s. Works like a charm, thanks and kudos to you!

  3. says

    How would one go about redirecting the Site address under the “general” tab? I want to redirect domain.com/ to domain.com/word – how would one go about 301′ing that? use .htaccess or?

  4. ShibaShake says

    You can just use the wp_redirect command if your headers are not sent out yet.

    Just add it to the functions.php file of your theme or child theme.

    if (check_for_something()) {
       wp_redirect('http://www.newlocation.com');
    }
    

    The location for wp_redirect has to be an absolute url.

Trackbacks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>