Setting cookie in WordPress

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

Read More
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);
            }
        });
    }

Related posts

1 comment

  1. Make sure to set the cookie path:

    setcookie('visionare_sound', $state, time()+60*60*24, '/');
    

    The cookie is being set…but likely not to your intended path.

Comments are closed.