Support » Developing with WordPress » Can you remove a plugin’s JS rendering, with a function?

Viewing 15 replies - 1 through 15 (of 23 total)
  • Whatever plugin you are using to add the scripts should be able to add them selectively. You should ask in the plugin’s support forum.

    Will do. WAs kinda wondering if there is a function that can pick it out, or a plugin that can go through them.

    Theoretically, you could do it with wp_dequeue_script

    But using the plugin’s interface, as suggested by Joy, is the right way to do it.

    There are those few we need to stop rendering on the homepage, though when I look in the source code I cannot find it.

    The plugins are by SmashBalloon I think, but I don’t see them changing it.

    How does that function you mention do it – can you advice? Is it based on the URL within our GT Metrix reports or some other way?

    Search for occurrences of wp_enqueue_script in the codebase of SmashBalloon’s plugin.
    If the JS files have been loaded by that plugin, then you would find somethink like:
    wp_enqueue_script ( 'custom-script-handle', 'PATH_TO_CUSTOM_SCRIPT' );

    where PATH_TO_CUSTOM_SCRIPT is the path to the script that you want to stop from loading, and custom-script-handle is the handle (identifier) of that script.

    Now you can dequeue that script with something like this:

    function wporg_11560356_dequeue_script() {
       wp_dequeue_script( 'custom-script-handle' );
    }
    add_action( 'wp_print_scripts', 'wporg_11560356_dequeue_script', 100 );

    Ok a few questions from this.

    1) how do we search for occurrences of that script in their codebase?
    2) how do we stop it loading, on a particular page?

    1. You have to download the plugin and open it in some code-editor like Notepad+=, SublimeText, Atom, VS Code etc.
    You can open it in a basic text editor too (like Notepad, if you are a Windows user), but then you would need to open all files one by one and search for the phrase ‘wp_enqueue_script’ in each of them

    2. FOr that you need to use the conditional tag (is_page())

    Ok bingo, found it.

    wp_enqueue_script( 'ctf_twitter_intents', 'https://platform.twitter.com/widgets.js', array(), CTF_VERSION, true );

    So let’s say I want to blog this from the homepage, how do I use your code with the conditional tag? Assuming it is based on the page id…?

    Something like

    function wporg_11560356_dequeue_script() {
      if ( is_front_page() ) {
        wp_dequeue_script( 'ctf_twitter_intents' );
      }
    }
    add_action( 'wp_print_scripts', 'wporg_11560356_dequeue_script', 100 );

    Read https://developer.wordpress.org/reference/functions/is_front_page/

    function wporg_11560356_dequeue_script() {
       if( is_page(YOUR_PAGE_ID) ) {
          wp_dequeue_script( 'ctf_twitter_intents' );
       }
    }
    add_action( 'wp_print_scripts', 'wporg_11560356_dequeue_script', 100 );

    So that dequeue is what removes it from the script, if it is the front_page?
    May I ask what the 11560356 means?

    No significance of 115603561 It was just used to make the function name unique. Since this WP forum topic had an id of 11560356, so I used it to make the function name unique

    Mmm it’s still saying the same warning in GTMetrix about it.
    I confirm this is on the homepage too.

    // disables certain third party scripts on homepage
    function wporg_11560356_dequeue_script() {
      if ( is_front_page() ) {
        wp_dequeue_script( 'ctf_twitter_intents' );
      }
    }
    add_action( 'wp_print_scripts', 'wporg_11560356_dequeue_script', 100 );

    This is at the bottom of our functions.php.

    Let’s try to print some debug statements in the function. to see if the function is actually being hit.

    // disables certain third party scripts on homepage
    function wporg_11560356_dequeue_script() {
      echo '<br> Inside the wporg_11560356_dequeue_script function call <br>';
      
      echo '<br> All enqueued scripts:  <br>';
    
      global $wp_scripts;
      foreach( $wp_scripts->queue as $handle ) :
        echo '<br>' . $handle . '<br> ';
      endforeach;
    
      if ( is_front_page() ) {
        echo '<br> Inside the if clause of the wporg_11560356_dequeue_script function call <br>';
        wp_dequeue_script( 'ctf_twitter_intents' );
    
        echo '<br> All enqueued scripts:  <br>';
    
        global $wp_scripts;
        foreach( $wp_scripts->queue as $handle ) :
          echo '<br>' . $handle . '<br> ';
        endforeach;
      }
    }
    add_action( 'wp_print_scripts', 'wporg_11560356_dequeue_script', 100 );

    We are trying to print the list of enqueued scripts twice:
    1. Just before dequeuing our target script
    2. Just after dequeuing it

    Compare the outputs to see whether there is any difference.

    what should I see on screen when I load the homepage?

Viewing 15 replies - 1 through 15 (of 23 total)
  • You must be logged in to reply to this topic.