Zbynek Nedoma
Zbynek Nedoma

Zbynek Nedoma

WordPress plugins dequeueing #1

WordPress plugins dequeueing #1

We need to start somewhere

Zbynek Nedoma's photo
Zbynek Nedoma
·Nov 8, 2022·

3 min read

Play this article

Plugins are very useful. You can create almost anything with them. The minor problem is that anyone can create a WordPress plugin but not everyone thinks about the PageSpeed of the site where it is used.

For a while I've been working on speeding up websites and removing unnecessary scripts from plugins is one part of it. During a workshop at WordCamp Europe 2022, I spoke with Adam Silverstein and we agreed that the only way to remove unnecessary styles and scripts from WordPress plugins is to ask the developers. So I got the crazy idea to install all the plugins that exist :). If a plugin loads some unnecessary assets into the front page after activation, I will write to the author to see if he could change it. If they respond, I'll add their response to the article. We'll see when I get tired of it :D

Why I am doing this?

From my many years of experience working with WordPress, I know that not all users know what happens when they activate a plugin. Sometimes they have a plugin activated that they don't use. Users should not be punished for activating a plugin by slowing down their site.

Something similar applies to plugin developers. It is very easy to write a plugin. But It is more difficult to write it so that it does not slow down the user's site. I don't want to mock anyone, because I know it's not easy. I would just like to point out small things that would be worth improving.

What I mean by unnecessary assets

Plugin should load their frontend assets (styles and scripts) only if the plugin is really used on that certain page. For example if you install plugin that allows you to create forms, it should load plugin assets only on page where the form is displayed. And not on all other pages.

Lets take the biggest ones first :D

Custom Twitter Feeds - wp repository

  • Style - ctf_styles (custom-twitter-feeds/css/ctf-styles.min.css)

This plugin loads only one css file after activation. You can see that in this function ctf_scripts_and_styles author is calling wp_enqueue_style twice for the ctf_styles style.

function ctf_scripts_and_styles( $enqueue = false ) {
    $options = get_option( 'ctf_options' );
    $not_ajax_theme = (! isset( $options['ajax_theme'] ) || ! $options['ajax_theme']);
    $font_method = 'svg';

    $loacalize_args = array(
        'ajax_url' => admin_url( 'admin-ajax.php' )
    );

    wp_enqueue_style( 'ctf_styles', plugins_url( '/css/ctf-styles.min.css', __FILE__ ), array(), CTF_VERSION );

    if ( $not_ajax_theme ) {
        wp_register_script( 'ctf_scripts', plugins_url( '/js/ctf-scripts.min.js', __FILE__ ), array( 'jquery' ), CTF_VERSION, true );
        wp_localize_script( 'ctf_scripts', 'ctf', $loacalize_args );
    } else {
        wp_localize_script( 'jquery', 'ctf', $loacalize_args );
    }

    if ( $enqueue ) {
        wp_enqueue_style( 'ctf_styles' );
        wp_enqueue_script( 'ctf_scripts' );
    }
}
add_action( 'wp_enqueue_scripts', 'ctf_scripts_and_styles' );

Possible solution will be to change the enqueue to register and move enqueueing to somewhere else. Maybe to function that is responsible for displaying. Let's see what the author says :)

Another plugin tomorrow (I hope :D)

Did you find this article valuable?

Support Zbynek Nedoma by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this