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.
Please try the below solution Using WordPress Init Hook
add_action('init', 'set_session');
function set_session() {
if(!session_id()){
session_start();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.