admin管理员组

文章数量:1122846

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: /$mypage?$newparam=$newvalue" );
?>

Which hooks are suitable for header redirects?

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 asked Mar 20, 2011 at 12:26 jnthnclrkjnthnclrk 1,8454 gold badges25 silver badges52 bronze badges 5
  • 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
Add a comment  | 

3 Answers 3

Reset to default 18

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.

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

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!

本文标签: plugin developmentWhich hook should be used to add an action containing a redirect