Cannot deregister a script using wp_deregister_script

I am trying to get rig of the Disqus script on my front page, but unfortunately I cannot manage how to do this.

Here is a little story of the steps I’ve done.

  1. Find the script name in the source code files of the plugin

    wp_register_script( ‘dsq_count_script’, plugins_url( ‘/media/js/count.js’, FILE ) );
    wp_localize_script( ‘dsq_count_script’, ‘countVars’, $count_vars );
    wp_enqueue_script( ‘dsq_count_script’, plugins_url( ‘/media/js/count.js’, FILE ) );

  2. Add an action for the wp_print_scripts hook

    add_action('wp_print_scripts', array($this, 'deregister_unused_scripts'), 100);
    
  3. Implement deregister_unused_scripts function

    public function deregister_unused_scripts()
    {
        wp_dequeue_script('dsq_count_script');
        wp_deregister_script('dsq_count_script');
    }
    

Still doesn’t work.

I also tried another hook

    add_action('wp_footer', array($this, 'deregister_unused_scripts'), 100);

But this didn’t help as well, I still get an output in the footer.

<script type="text/javascript">
/* <![CDATA[ */
var countVars = {"disqusShortname":"myname"};
/* ]]> */
</script>
<script type="text/javascript" src="http://myurl.net/wp-content/plugins/disqus-comment-system/media/js/count.js?ver=4.7.3"></script>

What can be wrong ?

EDIT

Here is the action used to register the plugin script.

add_action('wp_footer', 'dsq_output_footer_comment_js');

2 s
2

When attempting to dequeue a script, we need to hook in after the script is enqueued, but before it’s printed. In this case, the Disqus plugin is using the wp_footer hook at a priority of 10 to enqueue the scripts. The footer scripts get printed during wp_footer at a priority of 20. So we should be able to hook into wp_footer at a priority of 11 and dequeue the script.

add_action( 'wp_footer', 'wpse_262301_wp_footer', 11 );
function wpse_262301_wp_footer() { 
  wp_dequeue_script( 'dsq_count_script' ); 
}

Leave a Comment