1

I am building a shortcode plugin and I want to avoid loading unecessary code unless the shorcode is called in a page.

I am having trouble with the ajax functions. It seems I need to load everything very early in the plugin code, thus loading unecessary code for the whole site.

I am talking about the usual calls in adding ajax

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

and also the backend function needs to load.

function testFunction(){
        //do some server side thing with $POST
        echo 'this is a test function';
        die();
     }

If I don't load the add_action's at my plugin "top" code I don't get the ajax.js loaded If I don't load the testFunction at my plugin "top" code I get a call_user_func_array() expects parameter 1 to be a valid callback, function 'testFunction' not found or invalid function name

I have no problem loading the scripts "on demand" when shortcode is called, that is:

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 someone has had this issue too... Thank you in advance

  • You could test a couple hooks with the conditional has_shortcode() but I'm not sure reliable that will be when adding hooks since you need actual $post_content to test against. – Howdy_McGee Feb 19 '16 at 22:33
  • So you only want to add the ajax add_action and the testFunction if the shortcode exists on the page? – czerspalace Feb 19 '16 at 22:37
  • I think the problem is when did you call the wp_ajax hook, is it inside a function / class that's called on the file directly or through another hook? I think you will need to be more specific. When you need this ajax? or at least you can describe your code workflow – nonsensecreativity Feb 19 '16 at 22:47
  • Thanks. I will try the has_shortcode, even if it's not 100% reliable, at least it performs better – Rodolfo Cristovao Feb 20 '16 at 2:34
  • @nackle - the ajax hook action was called in the shortcode function. – Rodolfo Cristovao Feb 20 '16 at 2:43
0

I don't know if it is the 'best' place, but get_header is early enough to still enqueue scripts:

function at_set_header () {
    global $post, $has_the shortcode;

    if ( is_object( $post ) && has_shortcode( $post->post_content, 'shortcode_name') ) {
        $has_the_shortcode = true;
        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' ) ) );
    }
}
add_action( 'get_header', 'at_set_header');

The $has_the_shortcode global might be usefull to decide about other resources at later stages.

  • Thank you, I tried it and it sucessfully loads the ajax handles, however the reponse is always 0 no matter where I declare the testFunction. I tried plugin top code, inside the at_set_header, etc, it just seems the script can't see the testFuncion declaration that way? – Rodolfo Cristovao Feb 20 '16 at 3:08
  • What does the JS Ajax call look like? It needs to be a request to ajaxurl with parameter action=the_ajax_hook – ccprog Feb 20 '16 at 13:19
  • thank you @ccprog in my ajax.js <pre>jQuery.post(the_ajax_script.ajaxurl, { 'action': 'the_ajax_hook', data:'da_data', value:content_id } , function(response_from_the_action_function){ console.log(response_from_the_action_function); }</pre>... – Rodolfo Cristovao Feb 21 '16 at 22:22
  • Looks good so far. BTW, the ajax action needs to be declared top-code, since on the ajax call the get_header action will never be executed. Can you assert whether your testFunction gets called or not? You could i. e. try to log something with trigger_error. – ccprog Feb 22 '16 at 18:35
  • Hello @ccprog, I found that out too (also from czerspalace comment). So if I declare the ajax call blog-wide I have to declare the ajax php funcions too otherwise the function is never found by the ajax.js script when it loads. – Rodolfo Cristovao Feb 24 '16 at 1:02

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.