0

Question:

I have a website where the homepage exists out of 2 options to choose from:
Option 1: Familie
Option 2: Buiten seizoen

When a user clicks on one of the choices, a cookie has to be set. Remembering the choice as well. (the redirect to another page)

I have this now installed in functions.php:

function my_page_template_redirect() {

    if ( isset( $_COOKIE["landvanbartje_cookie"] ) ) {
        if( $_COOKIE["landvanbartje_cookie"] == "buiten_seizoen" ) {            
            wp_redirect( '/omgeving-rust-ruimte/' );
            exit;
        }
    }
    
        if ( isset( $_COOKIE["landvanbartje_cookie"] ) ) {
        if( $_COOKIE["landvanbartje_cookie"] == "familie" ) {            
            wp_redirect( '/homepage-gezinsvakantie/' );
            exit;
        }
    }

add_action( 'template_redirect', 'my_page_template_redirect' );
}

So I have set the redirects and named the cookies. Where and how do I install them on the 2 different pages? Anybody?

1 Answer 1

0

You can set the cookies on the two different pages by modifying your code to include the logic for setting the cookies when a user clicks on one of the options. Here's the updated code:

function my_page_template_redirect() {

    if ( isset( $_COOKIE["landvanbartje_cookie"] ) ) {
        if( $_COOKIE["landvanbartje_cookie"] == "buiten_seizoen" ) {            
            wp_redirect( '/omgeving-rust-ruimte/' );
            exit;
        }
    }
    
    if ( isset( $_COOKIE["landvanbartje_cookie"] ) ) {
        if( $_COOKIE["landvanbartje_cookie"] == "familie" ) {            
            wp_redirect( '/homepage-gezinsvakantie/' );
            exit;
        }
    }

    if (isset($_GET['choice'])) {
        $choice = sanitize_text_field($_GET['choice']);

        if ($choice == "buiten_seizoen" || $choice == "familie") {
            setcookie("landvanbartje_cookie", $choice, time() + 3600, '/');
        }
    }
}

add_action( 'template_redirect', 'my_page_template_redirect' );

In this code, we've added a new check to see if a query parameter named 'choice' is set in the URL. If it's set to either "buiten_seizoen" or "familie," it will set the corresponding cookie with that value.

To set the cookies on the two different pages, you can provide links or buttons on those pages that include the 'choice' query parameter in the URL. For example, on the "Familie" page, you can add a link or button like this:

<a href="?choice=familie">Choose Familie</a>

And on the "Buiten seizoen" page:

<a href="?choice=buiten_seizoen">Choose Buiten seizoen</a>

When a user clicks on these links/buttons, the 'choice' query parameter will be included in the URL, and the code we added will set the corresponding cookie accordingly.

1
  • I will try this code today, thank you very much already. Commented Jan 3 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.