8

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
  • At what hook and priority is the script being enqueued? Commented Apr 3, 2017 at 17:29
  • @NathanJohnson it is the default one = 10, please see the question update Commented Apr 3, 2017 at 17:41

2 Answers 2

15

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' ); } 
3
  • Awesome !! It works, thank you so much for the answer and explanation Commented Apr 3, 2017 at 18:47
  • For anyone else who read the solution too quickly and has set their priority to something greater than 20 (e.g. 999 or 1000 etc.) take special note that the author of the solution explicitly points out that scripts get printed at a priority of 20. Wasted 45 minutes because I was too eager to try implementing the solution. Commented Sep 8, 2020 at 0:36
  • Not working for me Commented Apr 12, 2022 at 7:24
1

add_action('wp_footer') didn't work for me, but it did 'wp_enqueue_scripts':

add_action( 'wp_enqueue_scripts', 'my_deregister_scripts', 1000 ); function my_deregister_scripts() { wp_deregister_script( 'wdm_script' ); wp_dequeue_script('wdm_script'); wp_deregister_script( 'enjoyHint_script' ); wp_dequeue_script('enjoyHint_script'); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.