I'm using the <a href="https://wordpress.org/plugins/upprev/">upPrev plugin</a> to create a flyout. However, I'd like to have the flyout not show up if a user has already hit the close button previously. I am also overriding the upPrev content (via a filter) with a newsletter form. If the user hits the submit button, I'd like the same cookie to be generated. That basically means I need a cookie generated onClick.
Then, whenever a single post page loads there should be a check for that cookie, and if the cookie is present then upPrev needs to be disabled.
Hariprasad Vijayan answers:
Hi,
Try this code in functions.php, it works in my localhost.
// Add script in footer for trigger close button click and call ajax function
add_action('wp_footer', '__script_upprev_close_click', 100);
function __script_upprev_close_click() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
echo '<script type="text/javascript">
jQuery( document ).ready(function() {
jQuery("#upprev_close").click(function(){
jQuery.post(
ajaxurl,
{
"action": "__callback_close_click",
"data": "hideit"
},
function(response){
}
);
});
});
</script>';
}
// Reading ajax call, and set cookie
add_action( 'wp_ajax___callback_close_click', '__upprev_set_cookie' );
add_action( 'wp_ajax_nopriv___callback_close_click', '__upprev_set_cookie' );
function __upprev_set_cookie() {
if($_POST['data'] && $_POST['data'] == 'hideit')
{
if (!isset($_COOKIE['hide_upprev'])) {
setcookie("hide_upprev", 'true', time()+3600, COOKIEPATH, COOKIE_DOMAIN);
}
}
die();
}
// Hide upPrev Box if cookie is set
add_action('init', '__hide_iworks_upprev_box',100);
function __hide_iworks_upprev_box()
{
if (!empty($_COOKIE['hide_upprev']) && $_COOKIE['hide_upprev'] == 'true') {
add_filter( 'iworks_upprev_box', '__callback__iworks_upprev_box' );
}
}
function __callback__iworks_upprev_box()
{
return '';
}
Let me know if you have trouble.
Regards,
Hariprasad
Joshua Nelson comments:
This seems to work, I had to make a couple of modifications to suit my form. I wanted to have it work on the form submit, but I can add the line for the javascript onclick specific to the form.
The other thing I just noticed is if I scroll down to the flyout, hit close, then scroll back up and down again - it returns! Only on the same page, it doesn't return on a reload (because then the cookie is initiated). Thoughts?
Hariprasad Vijayan comments:
Hi,
<blockquote>
The other thing I just noticed is if I scroll down to the flyout, hit close, then scroll back up and down again - it returns! Only on the same page, it doesn't return on a reload (because then the cookie is initiated). Thoughts?
</blockquote>
Add a line of code to ajax callback function
jQuery("#upprev_box").remove();
So, the code will be
add_action('wp_footer', '__script_upprev_close_click', 100);
function __script_upprev_close_click() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
echo '<script type="text/javascript">
jQuery( document ).ready(function() {
jQuery("#upprev_close").click(function(){
jQuery.post(
ajaxurl,
{
"action": "__callback_close_click",
"data": "hideit"
},
function(response){
jQuery("#upprev_box").remove();
}
);
});
});
</script>';
}
Joshua Nelson comments:
Great, this appears to be working! Thanks!