Using WP_DEBUG to Improve CSS and Style Enqueues in WordPress

Recently I saw a tweet conversation between @Norcross and @JPeterson about decaching css files when developing with WordPress. The solution is simple and something that WordPress even has built-in – however it’s also something that is easy to forget to turn on and off.

The suggested solution was to use the following when enqueuing css files:

function bm_enqueue_styles() {
    wp_enqueue_style( 'MY-CSS-NAME', MY-CSS-FILE.CSS, array(), time(), 'all' );
}

The important part is the time() function being used as the version number. Using this the css will be loaded clean on every page load. This is great when developing but when your code gets pushed out to production it is the sort of thing that can be forgotten about – which in this case would mean the css files will never be cached causing a performance hit. So is there a better way?

Enter WP_DEBUG

WP_DEBUG is a php constant used inside WordPress to enable error reporting to make things easier to debug. In my opinion all WordPress developers should be using this in their development environments. Switching it on enables PHP error reports – and displays messages when deprecated WordPress functions are being used. This means you can much more easily spot problems and make sure code is future proof.

It goes without saying that WP_DEBUG ‘should not’ be used in production environments. As such – when you’re developing it’s on, and when the site is live it’s off.

Note: This property should be added to your wp-config.php file

WP_DEBUG on

define( 'WP_DEBUG', true );

WP_DEBUG off

define( 'WP_DEBUG', false );

So how would I use it?

Firstly – I should point out something else I do in my themes. Instead of hard coding each version number for my custom scripts and styles – I use a php define. This then gives me a single place to clear cache for everything.

define( 'BM_VERSION', '1.0.1' );

function bm_enqueue_styles() {
    wp_enqueue_style( 'MY-CSS-NAME', MY-CSS-FILE.CSS, array(), BM_VERSION, 'all' );
    wp_enqueue_style( 'MY-CSS-NAME-2', MY-CSS-FILE-2.CSS, array(), BM_VERSION, 'all' );
}

Once you see this the solution should be obvious.

if ( WP_DEBUG ) {
    define( 'BM_VERSION', time() );
} else {
    define( 'BM_VERSION', '1.0.1' );
}

// other code and stuff ...

function bm_enqueue_styles() {
    wp_enqueue_style( 'MY-CSS-NAME', MY-CSS-FILE.CSS, array(), BM_VERSION, 'all' );
    wp_enqueue_style( 'MY-CSS-NAME-2', MY-CSS-FILE-2.CSS, array(), BM_VERSION, 'all' );
}

I must admit – before Norcross tweet I hadn’t considered using this for css file decaching – but I’ve now added it to the theme I’m running here on Binary Moon and it does make the process a little simpler.

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
04 Nov 2016

Automating WordPress Development with Gulp, Bash, and PHP

When I wrote about the things I had learned from releasing 20 WordPress themes, I mentioned that I had automated as much as I could. One of the things I automated was the build and deployment process for my themes...
26 Nov 2016

Generating a WordPress rtl.css with Gulp

Generating a rtl.css files is something I don’t enjoy doing. I think it’s really important that they are created since a large proportion of the world uses rtl (right to left) languages. But they are a bit of a pain...
13 Oct 2016

Lessons Learned from 20 Theme Releases on WordPress.com

In 2007 I partnered with Darren Hoyt to release Mimbo Pro, one of the earliest premium WordPress themes. In 2012 Mimbo Pro was published on wordpress.com. Last week – on October 5th 2016 to be precise – my 20th theme...
08 Aug 2013

The Death of WordPress Theme Frameworks

WordPress theme frameworks are on their way out. They’re dying a slow death. At least that’s what I think.Nathan Rice recently wrote an article with his thoughts about theme frameworks – in defense of them – however he clearly has...