admin管理员组文章数量:1129411
I have a tricky situation.... I'd like to check for a cookie. If it doesn't exist then then redirect to an internal wordpress page, and set a cookie. And then carry on browsing the site. But i get stuck in a loop if the url doesn't exist. This is what i have so far...any help would be great.
function cookiebasedredirect() {
// WHEN YOU HAVE FOUND YOUR COOKIE
if ( !isset($_COOKIE["sevisitor"])) {
setcookie('sevisitor', 1, time()+1209600, "/", "http://localhost/child/", false);
// GRABS THE CURRENT PAGE NAME - THIS IS ALSO KNOWS AS THE PAGE/POST SLUG
$pagename = get_query_var('pagename');
// PAGE CHECK SO THAT YOU ARE NOT IN AN INFINITE LOOP
// IN THIS SAMPLE MEDIA-GALLERIES IS THE PAGE YOU WANT TO BE
// REDIRECTED TO IF A COOKIE IS NOT SET, BUT ONCE YOU GET THERE
// MAKE SURE WORDPRESS DOESN'T EXECUTE THE REDIRECT
if( $pagename != "about-myself") {
wp_redirect( get_site_url().'/about-myself' ); exit;
} else {
}
} else {
}}
add_action("template_redirect", "cookiebasedredirect");
I have a tricky situation.... I'd like to check for a cookie. If it doesn't exist then then redirect to an internal wordpress page, and set a cookie. And then carry on browsing the site. But i get stuck in a loop if the url doesn't exist. This is what i have so far...any help would be great.
function cookiebasedredirect() {
// WHEN YOU HAVE FOUND YOUR COOKIE
if ( !isset($_COOKIE["sevisitor"])) {
setcookie('sevisitor', 1, time()+1209600, "/", "http://localhost/child/", false);
// GRABS THE CURRENT PAGE NAME - THIS IS ALSO KNOWS AS THE PAGE/POST SLUG
$pagename = get_query_var('pagename');
// PAGE CHECK SO THAT YOU ARE NOT IN AN INFINITE LOOP
// IN THIS SAMPLE MEDIA-GALLERIES IS THE PAGE YOU WANT TO BE
// REDIRECTED TO IF A COOKIE IS NOT SET, BUT ONCE YOU GET THERE
// MAKE SURE WORDPRESS DOESN'T EXECUTE THE REDIRECT
if( $pagename != "about-myself") {
wp_redirect( get_site_url().'/about-myself' ); exit;
} else {
}
} else {
}}
add_action("template_redirect", "cookiebasedredirect");
Share
Improve this question
asked Aug 4, 2011 at 3:45
WokWok
7422 gold badges7 silver badges16 bronze badges
2 Answers
Reset to default 2why not use the init
action hook:
function has_my_cookie()
{
if (!is_admin()){
//Check to see if our cookie is set if not redirect to your desired page and set the cookie
if ( !isset($_COOKIE["sevisitor"])) {
//setcookie
setcookie('sevisitor', 1, time()+1209600, "/", "http://localhost/child/", false);
//Redirect
wp_redirect( get_site_url().'/about-myself' ); exit;
}
}
}
add_action('init', 'has_my_cookie');
My final solution...
function cookie_redirect() {
// THE PAGE SLUG, YOU WANT TO BE REDIRECTED TO, WHEN THERE IS NO COOKIE
$pageslug = "about-myself";
// THE COOKIE NAME
$cookie_name = "thecookiemonster";
// CHECK IF YOUR COOKIE IS SET
if (!isset($_COOKIE[$cookie_name])) {
// SINCE THERE IS NO COOKIE, THEN SET IT
setcookie( $cookie_name, 1, time()+1209600, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
// GRABS THE CURRENT PAGE NAME - THIS IS ALSO KNOWN AS THE PAGE/POST SLUG
$pagename = get_query_var('pagename');
// MAKE SURE YOU ARE NOT AT THE PAGE YOU WANT TO BE DIRECTED TO, PREVENTS LOOP
if( $pagename != $pageslug) {
// REDIRECT US
wp_redirect( get_site_url() . '/' . $pageslug ); exit;
} else {
}
} else {
}
}
add_action( 'init', 'cookie_redirect');
本文标签: Wordpress Redirect based on the prescence of a cookie
版权声明:本文标题:Wordpress Redirect based on the prescence of a cookie 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736701797a1948461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论