1

In .js file of a theme, there are the 2 events using one function:

$j( window ).on( 'load', function() {   
    functionName();
} );

$j( document ).ajaxComplete( function() {   
    functionName();
} );

I would like to stop it from executing through either functions.php or my own plugin because I already have the same functionality coded. I don't want to edit the .js file of the theme. Is it doable at all?

2
  • you can remove the loading of this js file with wp_dequeue_script.
    – Kaperto
    Commented Dec 12, 2019 at 21:08
  • @Kaperto can you post your answer as an answer? Comments are for asking clarifying questions, not for posting answers. You can always flesh answers out after the fact
    – Tom J Nowell
    Commented Dec 12, 2019 at 21:54

2 Answers 2

2

We need to know how the script is being enqueued, but if it's done right, you will be able to remove that .js file using wp_dequeue_script.

How to correctly enqueue scripts

function my_register_script()
    //Registers a script to be enqueued later
    wp_register_script('script-handle', plugins_url('script.js', __FILE__));
}
add_action( 'wp_enqueue_scripts', 'my_register_script', 10);


function my_enqueue_script(){
    //Enqueue the script
    wp_enqueue_script('script-handle');
}
//hook to another action
add_action('other_action', 'my_enqueue_script', 10);

The wp_register_script register the script to be used, passing a $handle (id) and the script $src (url to the script). The wp_enqueue_script adds that script to our page.

It can also happen that the wp_register_script is not used. In that case, the $src is passed to the wp_enqueue_script like this.

wp_enqueue_script('script-handle', plugins_url('script.js', __FILE__));

How to remove a correctly registered script

If the script is enqueued correctly, then you can remove it from your functions.php passing the $handle to wp_dequeue_script.

wp_dequeue_script('script-handle');

Keep in mind that this function should be used after the script has been enqueued, so you should check to which action the wp_enqueue_script is hooked and run the wp_dequeue_script later, hooking it to the same action but with a higher priority.

Following the same example, the wp_enqueue_script is hooked to an action with a $priority of 10, so you should hook the dequeue with a bigger priority

function my_dequeue_script(){
    //Removes the enqueued script
    wp_dequeue_script('script-handle');
}
//hook to another action
add_action('other_action', 'my_dequeue_script', 11);

Where 11 in the add_action function is the $priority (Bigger means later execution)

Do not forget that there may be some scripts that depends of the script you are dequeuing. If thats the case, those scripts won't load.

3
  • The issue is that there are more functions used in this .js file. Trying to address the whole file will stop them as well. Commented Dec 14, 2019 at 12:44
  • @RyszardJędraszyk please clarify that on the question. It's going to be tricky if you can't dequeue the whole script. I think it is more a question of JS that WordPress. I would say it is completely WordPress unrelated.
    – Raba
    Commented Dec 17, 2019 at 15:17
  • You are right, your answer does indeed solve the case when the script is properly enqueued and the js file does just one specific thing. I will mark it as an answer and see if I can find a jQuery solution or get the developer to enqueue the relevant code separately. Commented Dec 17, 2019 at 15:43
0

I think you just want to remove one of the event listener. For this you could use wp_enqueue_script hook. Because most of the wordpress plugin and template files loads here. So add a inline javascript code to after jquery load.

add_action( 'wp_enqueue_script', 'unbind_js_event' );
function unbind_js_event() {
    $script = "
        $j( document ).ready( function() {
            $j( document ).unbind( 'ajaxComplete' );
            $j( window ).unbind( 'load' );
        });
    ";
    wp_add_inline_script( 'jquery', $script );
}
1
  • 1
    .unbind is deprecated since 3.0, besides this would remove everything attached to these event listeners not only the function I want to remove. Commented Dec 13, 2019 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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