0

I try to add class to body, and store it in php session. I use ajax to do it, but php session not store it. Variable is passing correct. Session is started in php function.

(function ($) {
    jQuery(document).on('click', '#wcag-button', function () {
        $('body').attr('id', 'wcag-theme');
        $('#wcag-button').hide();
        $('#wcag-button-off').show();

        var data = {
            'action': 'wcag_contrast_on',
            'bodyClass': 'wcag-theme'
        }
        $.post(Theme_Variables.ajax_url, data, function(response) {
            console.log(response);
        });
    });
})(jQuery);


add_action('wp_ajax_wcag_contrast_on', 'wcag_contrast_on');
add_action('wp_ajax_nopriv_wcag_contrast_on', 'wcag_contrast_on');

    function wcag_contrast_on() {
        session_start();
        $body_class=$_POST['bodyClass'];
        $_SESSION["usertemplate_css"] = $_POST['bodyClass'];
        //echo 'You sent ' . $body_class;
        echo ($_SESSION["usertemplate_css"]);
        $response['message']    = "Successfull Request";
        echo json_encode($response);
        wp_die();
    }

I also try to start session in header, but not working.

0

1 Answer 1

2

Please try the below solution Using WordPress Init Hook

add_action('init', 'set_session'); 
function set_session() {
 if(!session_id()){
      session_start();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.