Support » Developing with WordPress » Dequeuing Scropts

  • Resolved ante1974

    (@ante1974)


    Hi folks,

    I normally use plugins to add in custom JS or CSS, or use a theme’s inbuilt capabilities. However this time I tried to add a custom JS script with the WordPress enqueue function:

    function ae_add_custom_scripts_function() {
      wp_enqueue_script( 'js-file', get_template_directory_uri() . '/js/ae-bb-custom-jquery.js');
    }
    add_action('wp_enqueue_scripts','ae_add_custom_scripts_function');

    Ref: https://www.collectiveray.com/add-javascript-to-wordpress

    The script only has one function which I tested previously and it worked in a plugin that adds JS/CSS to each page of the site. That plugin was disabled and removed before I tried enquing.

    However, I must have added this to the wrong place in functions.php as it “breaks” my site and causes the page referenced to fire the function as soon as certain pages are loaded (2 of three pages in the artists menu).

    I tried removing the script, even deleting it from the server and that didn’t work! Then I realised I had to dequeue the script. I tried the technique here:

    https://pupungbp.com/how-to-remove-dequeue-deregister-script-on-wordpress/

    function deregister_ae_add_custom_scripts_function() {
    		wp_dequeue_script( 'ae_add_custom_scripts_function' );
    		wp_deregister_script( 'ae_add_custom_scripts_function' );
    		}
    		add_action( 'wp_print_scripts', 'deregister_ae_add_custom_scripts_function' );

    However, this isn’t working. Can anyone point me in the right direction here?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    To dequeue or deregister something, do not use the callback name, use the “handle” you used to enqueue.
    wp_dequeue_script( 'js-file' );

    No need to do so though, simply removing the initial add_action() call to enqueue will accomplish that. If the script is still referenced on the page after removing the call, it’s likely due to stale, cached pages. The caches need to be cleared. Could be your browser cache and/or one that’s server side.

    To get script to wait until the page finishes loading, jQuery’s .ready() callback is commonly used, or have your script listen for the document’s “onload” event. If your script has dependencies, to ensure they get loaded first you must list their handles in an array as a $deps (dependencies) arg when enqueued.
    wp_enqueue_script( 'js-file', get_template_directory_uri() . '/js/ae-bb-custom-jquery.js', array('jquery'));

    Thread Starter ante1974

    (@ante1974)

    Hi @bcworkz Thanks for this. You are right – I had a caching issue.

    I managed to dequeue, find the right place in functions.php to insert my code and all works well now.

    Many thanks.

    • This reply was modified 1 week, 2 days ago by ante1974. Reason: grammar
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.