Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Do you actually want to change the final URL or just the variables used in WP_Query? –  scribu Mar 20 '11 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 Mar 20 '11 at 14:03
    
scribu, I want to change final url and query string. –  jnthnclrk Mar 24 '11 at 15:03
    
t31os, I simplified my code for the question. The real thing contains more conditions. –  jnthnclrk Mar 24 '11 at 15:04
    
What was the outcome? Mark a solution pls. –  kaiser Oct 24 '11 at 12:56
add comment

3 Answers 3

up vote 4 down vote accepted

Like kaiser answered template_redirect hook is indeed appropriate for redirects.

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

share|improve this answer
    
Thanks for the suggestion, wouldn't mind knowing why wp_redirect is preferable, here: wordpress.stackexchange.com/questions/12944/… –  jnthnclrk Mar 24 '11 at 15:11
add comment

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

Example

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' );
share|improve this answer
add comment

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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