I want to build a plugin that grabs certain url params from the query string to build a new query string for the same page. I'm following the excellent the Professional WordPress Plugin Development book, but I'm not sure which hook to use for this action. Here is my action function:

add_action( 'init', 'tccl_redirect' );
function tccl_redirect() {
    header ( "Location: http://www.mysite.com/$mypage?$newparam=$newvalue" );

Which hooks are suitable for header redirects?

  • Do you actually want to change the final URL or just the variables used in WP_Query?
    – scribu
    Commented Mar 20, 2011 at 13:13
  • The code you've posted would basically redirect every page, is that what you want? Under what conditions should this redirection occur? NOTE: I've +1'ed kaiser anyway, template_redirect would also be my suggestion.
    – t31os
    Commented Mar 20, 2011 at 14:03
  • scribu, I want to change final url and query string.
    – jnthnclrk
    Commented Mar 24, 2011 at 15:03
  • t31os, I simplified my code for the question. The real thing contains more conditions.
    – jnthnclrk
    Commented Mar 24, 2011 at 15:04
  • 1
    What was the outcome? Mark a solution pls.
    – kaiser
    Commented Oct 24, 2011 at 12:56

3 Answers 3


Like kaiser answered template_redirect hook is indeed appropriate for redirects.

Also you should use wp_redirect() function, rather than setting header.


I'd say template_redirect. But take a look at the Action Reference.


Don't forget to exit() on redirect.

 * This example redirects everything to the index.php page
 * You can do the same for the dashboard with admin_url( '/' );
 * Or simply base the redirect on conditionals like 
 * is_*() functions, current_user_can( 'capability' ), globals, get_current_screen()...
 * @return void
function wpse12535_redirect_sample() {

    exit( wp_redirect( home_url( '/' ) ) );


add_action( 'template_redirect', 'wpse12535_redirect_sample' );

But i would say this example from kaiser cannot working, because after an redirect this hook template_redirect works again and again, you will have an endless forwarding !

Better is to check, if you're already on the homepage, like this:

function wpse12535_redirect_sample() {

    $current_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    $site_url = get_bloginfo('siteurl') . "/";

    if($current_url != $site_url)       
      exit( wp_redirect( home_url( '/' ) ));    

add_action( 'template_redirect', 'wpse12535_redirect_sample');

Works for me fine. Any suggestions? Regards!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.