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 badge1 Answer
Reset to default 1I 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
版权声明:本文标题:functions - Redirect specific page in Wordpress for first time visit 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741790577a2397633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论