WordPress AUTO POPUP for wp engine
function popup_cookie_reset() {
if(!empty($_GET['reset_cookie'])) { setcookie( 'popup_viewed', 0, time()+86400, COOKIEPATH, COOKIE_DOMAIN ); $_COOKIE['popup_viewed'] = 0; }
}
add_action('wp_loaded','popup_cookie_reset');
function popup_cookie_check() {
if(!empty($_COOKIE['popup_viewed'])) {
$popup_viewed_count = $_COOKIE['popup_viewed'] + 1; setcookie( 'popup_viewed', $popup_viewed_count, time()+86400, COOKIEPATH, COOKIE_DOMAIN );
}else{
setcookie( 'popup_viewed', 1, time()+86400, COOKIEPATH, COOKIE_DOMAIN );
}
$return['status'] = $_COOKIE['popup_viewed'];
echo json_encode($return);
die();
}
add_action( 'wp_ajax_popup_cookie_check', 'popup_cookie_check' );
add_action( 'wp_ajax_nopriv_popup_cookie_check', 'popup_cookie_check' );
<script>
jQuery(document).ready(function($) {
if (window.FormData) {
formdata = new FormData();
formdata.append("action", "popup_cookie_check");
}
if (formdata) {
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
response = JSON.parse(res);
//alert(response.status);
//alert(response.amount);
if (response.status < 2) { $('body').addClass('popup_on'); alert('popup_on'); }
}
});
}
});
</script>