Skip to Content
PHPCODE
Cookies ajax in WordPress
wordpress code / October 29, 2022
/*Start COOKIES SET*/
add_action("wp_ajax_close_emergency" , "close_emergency");
add_action("wp_ajax_nopriv_close_emergency" , "close_emergency");
function close_emergency(){
$repeat_announcement=get_field('repeat_announcement','option');
if(!empty($repeat_announcement)){
$repeat_ann_var=$repeat_announcement;
}else{
$repeat_ann_var=7;
}
setcookie( "close", "1", time() + (86400 * $repeat_ann_var),COOKIEPATH, COOKIE_DOMAIN, false );
}
/*End Close Button*/

Js in Cookies


jQuery(".announcement_msg_close").click(function(){
//jQuery(".header-emergency-msg").hide();
jQuery.ajax({
url: admin_ajax_url.admin_URL, // this will point to admin-ajax.php
type: 'POST',
data: {'action': 'close_emergency'},
success: function (response) {
}
});
});

AJAX Hook in Wodpress

<?php if(!empty($announcement_message_show)) { 
if(!empty($announcement_content)){ 
if(!isset($_COOKIE['close'])) {
if($_COOKIE['close'] == "") { ?>
<div class="announcement-section">
<p><?php echo $announcement_content .' '.button_group($announcement_link,'announcement-link');?></p>
<div class="close-button announcement_msg_close"></div>
</div>
<?php } } } } ?>
PHPCODE © 2024