最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Stop theme's jQuery function from running

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Dec 12, 2019 at 20:54 Ryszard JędraszykRyszard Jędraszyk 3244 silver badges17 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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.

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 );
}
发布评论

评论列表(0)

  1. 暂无评论