Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active February 18, 2022 11:13
Show Gist options
  • Save marklchaves/6ac0c9dcde6cc1692e97f1c99ff15e5a to your computer and use it in GitHub Desktop.
Save marklchaves/6ac0c9dcde6cc1692e97f1c99ff15e5a to your computer and use it in GitHub Desktop.
Launch a Popup Maker popup from a link, automatically close the popup after 8 seconds, and redirect to the link that originally launched the popup
<?php // Ignore this first line when copying to your child theme's functions.php file.
function auto_close_then_redirect()
{ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// ADD CUSTOM CODE HERE
let myRedirectURL = "/"; // Default to the homepage
// Listen for when the popup opens.
$('#pum-1208') // Change 1208 to your popup ID number.
.on('pumAfterOpen', function() {
try {
let trigger = $($.fn.popmake.last_open_trigger); // Get the link that was just clicked.
myRedirectURL = $(trigger).children('a')[0];
console.log(`[PUM] trigger link: ${$(trigger).children('a')[0]}`); // Remove for prod.
} catch (Error) {
console.error(`[PUM] Bailed because there's no click trigger`);
}; // try
setTimeout(function() {
PUM.close(1208); // Change 1208 to your popup ID number.
window.open(myRedirectURL, "_blank"); // Redirect
}, 8000); // Timeout after 8 seconds. Adjust as needed.
}); // listener
}); // jQuery
</script>
<?php }
add_action('wp_footer', 'auto_close_then_redirect', 500);
/**
* You can add the PHP code snippet to your child theme's functions.php file
* or with third-party plugins such as My Custom Functions and Code Snippets.
*
* Learn more:
* - https://docs.wppopupmaker.com/article/84-getting-started-with-custom-js
* - https://docs.wppopupmaker.com/article/552-getting-started-with-custom-php
*/
/**
* Example HTML for the click open link.
*
* <p class="popmake-1208 pum-trigger" id="sample-page-li-link" style="cursor: pointer;">
* <a href="https://twenty-twentyone.local/li-tips-sept-2016/" target="_blank" rel="noreferrer noopener">
* Get the Learning Indonesian PDF!
* </a>
* </p>
*
* See demo: https://share.wppopupmaker.com/xQuz8oE4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment