Set a cookie in WordPress, using a select form and Jquery

If you don’t give the cookie an expires time it will only be available during that session. You will also need to destroy an existing cookie if it is already set.

jQuery(function() {

    jQuery('#categoriesform').submit(function(e) {
         if (jQuery.cookie('my_cookie') ) { jQuery.cookie( 'my_cookie', null) }
        jQuery.cookie('my_cookie', 'my_value', { expires: 30 });
    });
});

Have you tested to see if the cookie is being set? Use the network tab in chrome developer tools and load the page. Click on the first item on the left which will be the url of your site then click on cookies tab and see if the cookie is there.

When reading your cookie you should also check to see if it’s there.

if (isset($_COOKIE['my_cookie'])) {
$termID = $_COOKIE['my_cookie'];

Leave a Comment

tech