I’ve been tinkering on this and can’t seem to spot the problem. I’m able to get the state right but it’s not saving in the cookie.
functions.php
add_action( 'wp_ajax_setBgSoundCookie', 'so_wp_ajax_function' );
add_action( 'wp_ajax_nopriv_setBgSoundCookie', 'so_wp_ajax_function' );
function so_wp_ajax_function(){
$state = $_POST['state'];
setcookie("bg_sound", $state ,time()+60*60*24); //expires in 24 hours
wp_die();
}
myscript.js
function setSoundCookie(state){
$.ajax({
url: vision_globals.ajaxurl,
type: 'POST',
data: {
action :'setBgSoundCookie',
state : state
},
success: function (data) {
},
error: function (er) {
console.log(er);
}
});
}
Make sure to set the cookie path:
The cookie is being set…but likely not to your intended path.