Site icon Hip-Hop Website Design and Development

Finest place to load wp_ajax_the_ajax_hook motion in plugin

I’m constructing a shortcode plugin and I wish to keep away from loading unecessary code except the shorcode known as in a web page.

I’m having hassle with the ajax capabilities. It appears I must load every little thing very early within the plugin code, thus loading unecessary code for the entire website.

I’m speaking in regards to the regular calls in including ajax

add_action( 'wp_ajax_the_ajax_hook', 'testFunction' );
add_action( 'wp_ajax_nopriv_the_ajax_hook', 'testFunction' ); 

and likewise the backend perform must load.

perform testFunction(){
        //do some server facet factor with $POST
        echo 'this can be a take a look at perform';
        die();
     }

If I do not load the add_action’s at my plugin “prime” code I do not get the ajax.js loaded
If I do not load the testFunction at my plugin “prime” code I get a call_user_func_array() expects parameter 1 to be a sound callback, perform ‘testFunction’ not discovered or invalid perform identify

I’ve no drawback loading the scripts “on demand” when shortcode known as, that’s:

wp_enqueue_script( 'my-ajax-handle', FAU_PLUGIN_URL . 'js/ajax.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

Hope somebody has had this situation too… Thanks prematurely