admin管理员组

文章数量:1336008

I tried the following to redirect one page on the first visit:

function check_for_redirect() {
    if( is_page( 8219 ) )
    $days_to_expire = 30;

    if (!is_admin() && !isset($_COOKIE['already_visited'])) {
        setcookie('already_visited', true, time() + 86400 * $days_to_expire);
        wp_redirect('/welkom', 302); //change the URL and status to whatever you want
        exit;
    }
}
add_action('init', 'check_for_redirect');

I get a Redirect loop if i use this one. How to make this work?

I tried the following to redirect one page on the first visit:

function check_for_redirect() {
    if( is_page( 8219 ) )
    $days_to_expire = 30;

    if (!is_admin() && !isset($_COOKIE['already_visited'])) {
        setcookie('already_visited', true, time() + 86400 * $days_to_expire);
        wp_redirect('/welkom', 302); //change the URL and status to whatever you want
        exit;
    }
}
add_action('init', 'check_for_redirect');

I get a Redirect loop if i use this one. How to make this work?

Share Improve this question edited Jan 30, 2021 at 14:59 Ruvee 1641 gold badge1 silver badge10 bronze badges asked Jan 29, 2021 at 14:42 RensRens 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

I would always hook it to template_redirect. Something like this:

add_action( 'template_redirect', 'custom_check_for_redirect' );

function custom_check_for_redirect(){
  global $wp_query; # It could work without this too!
  
  if( is_page( 8219 ) )
    $days_to_expire = 30;

    if (!is_admin() && !isset($_COOKIE['already_visited'])) {
        setcookie('already_visited', true, time() + 86400 * $days_to_expire);
        wp_redirect(home_url('/welkom'), 302);
        exit;
    };

};

本文标签: functionsRedirect specific page in Wordpress for first time visit