0

I'm having some conflicts I think with a couple of plugins I have written that use ajax. As they load their resources on every page rather then just when needed I think it's causing problems.

What's the best way to just load a plugin when you need it? Both plugins in question only need to run when the user navigates to a single page

I just wanted to wrap a plugin in something like:

if (is_page( 'slug-name' ) ) {

     //plugin code

}

But that obviously won't work because it's outside of any Wordpress hooks.

WooCommerce has the following that only loads your WooCommerce plugin if WooCommerce itself is active, could I adapt this some how?

/**
 * Check if WooCommerce is active
 **/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option(     'active_plugins' ) ) ) ) {
    // Put your plugin code here
}
1

figured it out... The main problem with the scripts so just wrapped them in this for each plugin:

function my_enqueue_javascript() {
    if (is_page('page')) {
        //Include Javascript library
        wp_enqueue_script('', plugins_url( '/js/ajax.js' , __FILE__ ) , array( 'jquery' ));
        // including ajax script in the plugin Myajax.ajaxurl
        wp_localize_script( '', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));
    }
}
add_action( 'wp_print_scripts', 'my_enqueue_javascript', 100 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.