0

I have code to add a cookie to a page, it is very simple:

function setup_user() {
    if (is_page(21)) {
        setcookie("user_type", "advanced", time()+157680000);
    }
}
add_action( 'get_header', 'setup_user');

if the user goes to a certain page, they are an advanced user and do not need to see basic help items. So now I want to hide some content on some pages.

I currently use the Quotes plugin to manage the content that should be shown/hidden. I know I can add conditional logic:

if(!isset($_COOKIE["user_type"])) {
    // then the content should not show.
}

How can I check that cookie, and based on it show/hide content on any page of the site?

3

An idea is to use some CSS if you want to apply this logic to many elements on different pages.

You can add this code to header.php for example :

if(!isset($_COOKIE["user_type"])) {
    <style>.hide_content {display:none!important}</style>
}

Then you can simply add the CSS class to all elements you want to hide when cookies are set.

  • That is a fantastic idea!! Going to try it. – nycynik Nov 6 '17 at 20:30
  • 1
    @nycynik and let me know if it's working fine ;) – Temani Afif Nov 6 '17 at 20:31
  • This works great! Very easy to implement, and works on everything, this was a brilliant suggestion, saved me a ton of time! – nycynik Nov 6 '17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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