Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally opening Popup Maker popup in wordpress with jQuery

I am trying to conditionally load a popup in Wordpress. The popup is configured in the plugin Popup Maker and has a CSS class of "popmake-2503".

I've created a function in my theme's functions.php which loads a script conditionally:

// open popup
function open_popup() {
if( is_user_logged_in() && is_page('checkout') )
    {
    wp_enqueue_script(
        'open-popup',
        get_stylesheet_directory_uri() . '/open-popup.js',
array( 'jquery' )
            );
        }
    }
add_action( 'wp_footer', 'open_popup' );

In the open-popup.js file, I have the following:

jQuery(document).ready(function($) {
    alert ('running');
    $('#popmake-2503').popmake('open');
});

I have read the API but still I cant get the popup to fire, and no error shows in the console. The basic javascript alert works.

Thank you.

EDIT1: the other way around works, i.e.,

$('#popmake-2503').popmake('close');

works for effectively closing the popup after it is automatically loaded (as defined in the Popup Maker configurations in WP admin).

like image 310
BMM Avatar asked Jan 22 '16 10:01

BMM


People also ask

How do I use the shortcode in pop-up maker?

Setting Up the Shortcode. The 'Popup Trigger' shortcode is accessible on the content editor 'Visual' tab from the Popup Maker Shortcode Button. When the 'Popup Trigger' option is selected, a 'Popup Trigger' option settings box will display.

How do I edit pop-up maker in WordPress?

From the WordPress Admin, go to Popup Maker >> Settings >> General, and select a saved popup theme from the drop-down menu. When Popup Maker is first activated on a site, the default popup theme is set to Default Theme. Each of the popup themes preinstalled with Popup Maker will be listed as menu options.


1 Answers

Your solution is correct but currently Popup Maker defers js loading so your code runs before the .popmake('open') function has been loaded and the popups initialized.

We are going to be addressing that soon, but try adding a short timeout before opening it:

jQuery(document).ready(function($) {
    setTimeout(function() {
        $('#popmake-2503').popmake('open');
    }, 2000);
});

or better yet hook into the pumInit event like so. That way it opens as soon as the popup is actually ready. This is also safe to load in the head as it uses .on event binding

jQuery(document).on('pumInit', '#popmake-2503', function () {
    PUM.open(2503); // Newer method than $('#popmake-2503').popmake('open'); and uses internal selector caching.
});

And lastly if you want a reusable solution you can create custom conditions that can be used in our popup condition editor. You only need one condition to do both true & false checking. The following doc includes multiple complete examples you can use:

PS I'm the lead developer for PM so this is the defacto best answer ;)

like image 112
Daniel Iser Avatar answered Sep 25 '22 14:09

Daniel Iser