• I want to ask what the best practices are for enqueueing and dequeueing stylesheets in plugins that you create. For myself, i ran across a situation in a project where i built multiple plugins that used different styles and ran across situations where ‘plugin2/style2’ for example was still using styles from ‘plugin1/style1’ and i realized that was because i hadn’t DEqueued the stylesheet from ‘plugin1/style1’.

    So, my question is what are the WP recommended best practices for when and how best to dequeue styles/scripts, both if you’re working with a child theme and not. I would like to make sure that i do this by the book.

    thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The recommended way is described in the manual:
    https://developer.www.ads-software.com/reference/functions/wp_dequeue_style/

    Note that you can control which hook is executed first via the priority of add_action. If you want to dequeue something, you could use PHP_INT_MAX as priority, which will definitely make the dequeuing happen at the end after all other hooks.

    Thread Starter jlanpheer

    (@jlanpheer)

    Thanks you for your reply, i had already seen this. I guess what i’m unclear about is when/where to do this/put this code. If you are working with a child theme, i guess you might put this code in the child theme’s functions.php file. Where would you do this if you DON’T have a child theme? If you are writing a plugin which uses its own custom style, i guess i’m unclear on where the dequeueing is done. It seems to me that ‘cleaning up your mess before leaving’ is ‘good style’! :-). Thank you for your help!

    You can also use these codes in a plugin. Just paste them into the central file of the plugin. Or a file that is loaded from it. There are no differences compared to the theme (except for the other paths that point to the CSS files).

    Thread Starter jlanpheer

    (@jlanpheer)

    OK, i tried that and i guess i’m just not understanding something, so i will give a code example. I’ve built two plugins representing two custom-built pages of a site, we’ll call them ‘plugin_page1’ and ‘plugin_page2’. In each one, i have enqueued the style for that page something like this:

    function create_plugin_page1_header() {
    
            require_once(ABSPATH . 'wp-load.php');
            require_once( ABSPATH . 'wp-config.php' );
    
            wp_enqueue_style(
                'plugin1-style',
                plugin_dir_url( __FILE__ ) . 'css/plugin1.css',
                array(),
                1,
                'all'
            );      
    }
    add_action('wp_enqueue_scripts', 'create_plugin_page1_header');
    

    The second plugin’s style was instantiated in the very same way, just in its own php file (replace ‘plugin1’ with ‘plugin2’). Also, in each plugin’s main file, i have placed the following code (again, slightly different to accommodate that plugin’s specifics):

    
    add_action( 'wp_enqueue_scripts', 'plugin1_deregister_styles', 11 );
    function plugin1_deregister_styles() {
    	wp_dequeue_style( 'plugin1-style' );
    }

    I’ve set the priority higher on the deregister function and i’ve done this in both plugins 1 and 2. A problem (not the only problem) is that when i load the page powered by plugin1, it does not have the styling that i desire, it has the styling from plugin2. And indeed, when i look at the header code in Chrome’s dev tools, the style for plugin1 is not registered at all, but the style for plugin2 (a page that i’ve not visited…yet) HAS been enqueued/registered.

    So, i’m clearly missing a key concept here. Of course, i want to fix this code for my project, but i also want to understand the design pattern so that when i build plugins and work with child themes in the future, that i do it correctly and by the book.

    Maybe I’m missing something, but: what’s the point of using 2 plugins that take away each other’s styles?

    Thread Starter jlanpheer

    (@jlanpheer)

    I have two custom pages of a site that i am building, each with their own custom styling requirements. In this case, the ‘about’ page has its own styling requirements that are completely different from the ‘contact form’ page. I guess that i could put all of it together in one plugin, but i was trying to keep them separate for potential future re-use. I have at least two more pages coming that will also have their own unique style quirks as well, but i’m not there yet. I want to understand and implement the design pattern first.

    Then I would recommend you to solve this rather via child theme. What speaks against it for you?

    Thread Starter jlanpheer

    (@jlanpheer)

    To further clarify, some of the styling elements DO remain the same from page-to-page (fonts, color palette, etc….), but there will be up to five custom pages, i haven’t found a template that will accommodate what we’re after, and i’ve resolved to build it out myself. And, i’m actually mostly done, save for the styling. Maybe there’s another way that i should go, or could have gone. It all started with my frustrations with contact_form_7, which i simply couldn’t get it to do what my client needed. I’m open to other ideas and maybe there’s a ‘right way’, but this way is working…. up to this point.

    Thread Starter jlanpheer

    (@jlanpheer)

    I’m not at all sure what you mean by solving this via the child theme. I have chosen the Astra theme (supposedly for its ‘customizability’) and created a child theme from it. Are you suggesting that i cannot do this: (1) load a page, (2) enqueue a style for that page in the header, (3) serve the page, (4) dequeue that style? Maybe “I’m” missing something, i thought that was was the process of enqueueing and dequeueing was all about….

    I’ve essentially created shortcodes for each of my pages and each shortcode has its own plugin that represents the functionality of a page.

    • This reply was modified 1 year, 9 months ago by jlanpheer.

    You don’t have to dequeue styles all the time. You could include styles only on the single specific page. In the simplest case you do this within the wp_enqueue_scripts hook via

    if( get_the_ID() = 42 ) {
     // enqueue here
    }

    You can do this both in a plugin and theme.

    However, a child theme would have an advantage: when you edit a page in the block editor, you can select the template to display on the right. Then you don’t have to do that in programming or via shortcode but could have it editorially assigned. The rest of your points (apart from the not necessary dequeue) is also implementable with it.

    Thread Starter jlanpheer

    (@jlanpheer)

    Thank you for your suggestions and taking the time to offer advice, that is MUCH appreciated! I will play with some of your suggestions and report back.

    I guess what is most baffling to me at the moment is that currently is how WordPress enqueues these styles. In my current case, i’ve got ‘style1’ associated with ‘plugin/page1’ and ‘style2’ associated with ‘plugin/page2’. When i test how ‘page1’ appears once it is served, SOMEHOW the style for ‘plugin2’ has been instantiated even though i have not even submitted a request for page 2 yet. I don’t understand how a style for a page can be enqueued without the page having even been requested. As i was pondering this last night, i guess that is the thing that i perhaps truly do not understand. That makes no sense to me at the moment.

    Thank you for sharing your insights!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Best practices for dequeueing styles in plugins’ is closed to new replies.