Register Most Recent Style

When you register a stylesheet, you can specify the version number. But this is just appended to the end of the file name. WordPress will enqueue whichever version of that stylesheet was enqueued last, regardless of which is newer.

When you use be_register_latest_style() instead of wp_register_style(), it will check to see if that style has already been registered. If it has, it will compare the versions and use the latest version as the registered one.

This is useful for common stylesheets used by many themes and plugins, like Genericons.

<?php
/**
* Register Latest Style
*
* @author Bill Erickson
* @link http://www.billerickson.net/code/register-recent-style/
*/
function be_register_latest_style( $handle, $src, $deps, $ver, $media ) {
global $wp_styles;
if( !isset( $wp_styles->registered[$handle]->ver ) || version_compare( $ver, $wp_styles->registered[$handle]->ver ) ) {
wp_deregister_style( $handle );
wp_register_style( $handle, $src, $deps, $ver, $media );
}
}
// Example of Usage
function be_enqueue_styles() {
be_register_latest_style( 'genericons', get_stylesheet_directory_uri() . '/css/genericons/genericons.css', false, '3.0.2' );
wp_enqueue_style( 'genericons' );
}
add_action( 'wp_enqueue_scripts', 'be_enqueue_styles' );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk