it-swarm.com.de

Wordpress Redirect basierend auf dem Vorhandensein eines Cookies

Ich habe eine schwierige Situation ... Ich möchte nach einem Cookie suchen. Wenn dies nicht der Fall ist, leiten Sie zu einer internen WordPress-Seite weiter und setzen Sie ein Cookie. Und dann surfen Sie weiter auf der Website. Aber ich stecke in einer Schleife fest, wenn die URL nicht existiert. Das ist, was ich bisher habe ... jede Hilfe wäre großartig.

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");
2
Wok

warum nicht den init action hook benutzen:

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

Meine endgültige Lösung ...

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