0

I am using a plugin which enqueues a javascript file in one of its shortcode functions (which I run on a page).

I don't want this file to be loaded.

So I tried using wp_dequeue_script in my functions.php file but this didn't work.

Perhaps my functions.php file is loaded before the shortcode that's in the page? How can I get around this issue?

The shortcode function:

global $dwqa, $dwqa_sript_vars, $script_version; ob_start(); $dwqa->template->remove_all_filters( 'the_content' ); echo '<div class="dwqa-container" >'; dwqa_load_template( 'question', 'submit-form' ); echo '</div>'; $html = ob_get_contents(); $dwqa->template->restore_all_filters( 'the_content' ); ob_end_clean(); wp_enqueue_script( 'jquery-ui-autocomplete' ); wp_enqueue_script( 'dwqa-submit-question', DWQA_URI . 'templates/assets/js/dwqa-submit-question.js', array( 'jquery', 'jquery-ui-autocomplete' ), $script_version, true ); wp_localize_script( 'dwqa-submit-question', 'dwqa', $dwqa_sript_vars ); return $this->sanitize_output( $html ); 

How can I not load this particular js file?

2
  • This is a bit of a guessing game if you don't include the code that is inserting the unwanted js file
    – cjbj
    CommentedSep 28, 2016 at 10:24
  • I don't understand, the code simply calls wp_enqueue_script. I will update my question, but the function is submit_question_form_shortcode in this file: github.com/designwall/dw-question-answer/blob/master/inc/…
    – theyuv
    CommentedSep 28, 2016 at 10:34

1 Answer 1

2

When shortcodes are evaluated, the header of the site has already been assembled. So if the shortcode wants to enqueue a script it must do so in the footer (by setting the last parameter to wp_enqueue_script to true.

At this point you're still not too late to prevent the file from actually being included in the footer. Normally, in functions.php you would enqueue or (de)register scripts with the after_setup_theme hook. However, if you look at the order in which actions are executed you would be trying to deregister a script long before it is enqueued.

So, you would have to find a hook that fires after the shortcode has enqueued the script. The logical choice would be wp_footer, like this:

add_action ('wp_footer','wpse240803_deregister_dwqa'); function wpse240803_deregister_dwqa() { wp_deregister_script ('dwqa-submit-question'); } 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.