Duplicated CSS files using wp_enqueue_style()

Naming conventions for scripts and styles have always been a difficult issue in WordPress plugins, with many different conventions used and often causing conflicts. I’ll try to give my view on this matter, and elaborate where necessary.

Let me start off by stating my opinion on the example in your question: in my opinion, only fontawesome is a suited handle.

First of all, let me differentiate between plugin-specific and non-plugin-specific scripts and styles. The former group consists of scripts and styles that are only useful to that specific plugin or theme, whilst the latter consists of all scripts and styles that can be used across products (in most cases, WordPress stylesheets or JavaScript libraries).

Plugin-specific scripts and styles

In my opinion, plugin-specific scripts and styles should always be prefixed with a plugin prefix. For example, for Advanced Custom Fields, an admin script that needs to be included on all admin pages could be registered as acf-admin (using wp_register_script( 'acf-admin', .. )).

Non-plugin-specific scripts and styles

This is a more difficult choice, and there are several considerations that have to be made:

  1. Should I add a version number to the handle?
  2. Should I prefix it with my plugin prefix?
  3. Should I prefix it with its parent library name?

In my opinion, the answer to questions one and two are a clear “no”.

Should I add a version number to the handle?

Multiple versions of the same script or style are in just about all cases not intended to co-exist in a single page. Adding a version number to the handle of a script or style implies that each product version is an entirely different product. This is also how WordPress does it: the jQuery library has the handle “jquery”, and not “jquery-[version]”.

Should I prefix it with my plugin prefix?

Plugins and themes should all rely on a single version of a script or style. Prefixing a handle with a plugin prefix implies that the script or style is product-specific, which it isn’t. This is exactly the case in your example: each product deems the script specific to their product, whilst it is, in fact, more of a library.

Adding a version number or plugin prefix to the handle of a script or style eliminates a big part of the use of registering scripts and style with a unique handle.

Should I prefix it with its parent library name?

If a library is fully dependent upon a single library and thereby more of an extension to a library, it should be prefixed with the “parent” library name or prefix. If you have created a jQuery plugin called “inline-edit”, for example, you should use the handle jquery-inline-edit. This is done the same way in WordPress core: the Sortable library for jQuery is called “jqeury-sortable” and not just “sortable” (as other, non-jQuery-dependent sorting libraries might very well exist).

Leave a Comment

error code: 521