Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created March 2, 2018 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/8f9d93139dbb2086733e28d2187ae0b5 to your computer and use it in GitHub Desktop.
Save aaronsummers/8f9d93139dbb2086733e28d2187ae0b5 to your computer and use it in GitHub Desktop.
Show popup once per user
<?php
global $prefix;
$popup_switch = rwmb_meta("popup_switch");
if ( $popup_switch == 1 ) :
$popup_image = rwmb_meta( "{$prefix}popup_image", array( 'size' => 'popup' ) );
$popup_title = rwmb_meta("{$prefix}popup_title");
$popup_desc = rwmb_meta("{$prefix}popup_desc");
$popup_button = rwmb_meta("{$prefix}popup_button");
$button_link = rwmb_meta("{$prefix}button_link");
$button_url = ( $button_link == 'internal' ) ? get_the_permalink(rwmb_meta("{$prefix}popup_button_internal")) : rwmb_meta("{$prefix}popup_button_external");
?>
<div id="popup-<?php echo get_the_ID(); ?>" class="popup">
<div class="popup-inner" style="background-image: url(<?php echo $popup_image['url']; ?>)">
<div class="popup-text-wrapper">
<h2><?php echo $popup_title; ?></h2>
<?php echo wpautop($popup_desc); ?>
<a href="<?php echo $button_url; ?>" class="button white"><?php echo $popup_button; ?></a>
<p><a href="#" class="void close">Close X</a></p>
</div><!-- /.popup-text-wrapper -->
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script>
jQuery(document).ready(function($) {
$('.close').on('click', function() {
$('.popup').fadeOut(300);
})
// initially popup is hidden:
$('#popup-<?php echo get_the_ID(); ?>').hide();
// Check for the "#popup-<?php echo get_the_ID(); ?>" cookie, if not found then show the dialog and save the cookie.
// The cookie will expire and every 1 day and the dialog will show again.
if (Cookies.get('popup-<?php echo get_the_ID(); ?>') == null) {
// Create expiring cookie, 1 days from now:
Cookies.set('popup-<?php echo get_the_ID(); ?>', 'yes', { expires: 1, path: '/' });
// Show dialog
$('#popup-<?php echo get_the_ID(); ?>').fadeIn('300');
}
});
</script>
<?php
endif; //end if ( $popup_switch == 1 ) :
?>
@aaronsummers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment