Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Programming / WordPress Page Redirect

WordPress Page Redirect

by ShibaShake 11 Comments

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!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. Marcelo says

    February 21, 2016 at 4:25 pm

    Thanks man, great help.

    Reply
  2. Ritshidze says

    August 11, 2014 at 2:55 am

    Thank You

    Reply
  3. Chris says

    December 2, 2013 at 6:53 am

    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’ );

    Reply
  4. Gert-Jan says

    October 3, 2012 at 11:56 pm

    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!

    Reply
  5. Anime says

    November 9, 2011 at 12:21 am

    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?

    Reply
    • ShibaShake says

      November 22, 2011 at 11:20 am

      If you want to do a permanent redirect, I would use .htaccess.

      Reply
  6. swaan says

    January 20, 2011 at 1:21 am

    So here I am searching for methods to log and convert 404 urls into 301 redirects and your first link in the article returns a 404 😀 😀

    Reply
    • ShibaShake says

      January 20, 2011 at 9:05 am

      I purposely did it to see if people were paying attention. 😉

      Thanks for letting me know. It is fixed. 😀

      Reply
  7. ShibaShake says

    February 15, 2010 at 8:28 am

    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.

    Reply
  8. genux says

    February 15, 2010 at 2:25 am

    How do you add a wp_redirect before the headers are sent out ? which plugin do you add to to check something so that you can redirect.?

    Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·