Remove unused assets

This topic contains 4 replies, has 2 voices, and was last updated by  Alex Rollin 6 years ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #454604

    Andersen
    Expired Member
    Post count: 49

    Hi GD team,

    im working on making my website slimmer by removing any unused assets that would cause a http request. I did notice that GD loads on all pages and im not that well-known on how to do it, but if you could somehow just load assets on pages that actually require styling and javacscripts it would improve performance for my website quite alot. Its so difficult to identify which css or js i can disable (i use perfmatters for disabling assets on each page),

    I did find a blog were they state the issue and how they solved it but im not sure if that would work for you:

    Source: https://www.cozmoslabs.com/58990-remove-unused-css-js-files-in-wordpress/

    LOAD ASSETS ONLY IF SHORTCODE IS EXECUTED
    We’ve been using this technique on almost all our plugin assets for quite some time.

    Basically, we have a global variable that gets initiated in our shortcode, and if it’s set and true, we’re loading our assets like so:

    // create a shortcode that registers a shortcode
    function cozmoslabs_some_shortcode( $atts ) {
    global $cozmoslabs_some_shortcode;
    $cozmoslabs_some_shortcode = true;
    return ‘We have a shortcode’;
    }
    add_shortcode(‘cozmoslabs_short’, ‘cozmoslabs_some_shortcode’);

    function cozmoslabs_enqueue_script() {
    global $cozmoslabs_some_shortcode;
    if ( isset($cozmoslabs_some_shortcode) && $cozmoslabs_some_shortcode == true ) {
    wp_enqueue_script( ‘my-js’, ‘filename.js’, true );
    }
    }
    add_action( ‘wp_footer’, ‘cozmoslabs_enqueue_script’, 90000 );
    Unfortunately, there are drawbacks to this as well:

    the scripts need to be added to wp_footer with a late priority because if we add them to wp_enqueue_scripts, our global variable has a good chance not to be set
    if you need something added in the header of the site, this won’t work
    in wp_enqueue_script() you have to set $in_footer to true

    #454607

    Andersen
    Expired Member
    Post count: 49

    From comments

    There’s actually no need to create a global variable (and you shouldn’t) if you want to only load scripts/styles on pages where the shortcode is used. One method is to use wp_register_script() to register the script in the wp_enqueue_scripts hook, and then to use wp_enqueue_script(‘script-hook’) in the plugin callback. The script will then only be loaded if the plugin callback is called.

    #454684

    Alex Rollin
    Moderator
    Post count: 27815
    #455296

    Andersen
    Expired Member
    Post count: 49
    This reply has been marked as private.
    #455297

    Alex Rollin
    Moderator
    Post count: 27815
Viewing 5 posts - 1 through 5 (of 5 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount