3

I am using Font Awesome on my theme.

wp_enqueue_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css');

I am using another plugin that uses Font Awesome, but its using an old version.

wp_enqueue_style( 'fontawesome', '//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css', array(), MSI_VERSION, 'all' );

The two methods are using the same handle so only one gets rendered in the HTML... and that is the plugin's old version. I could change my handle but then two Font Awesomes would be loading. I could hack the plugin, but, EWWW!

How do I get my version of Font Awesome to take priority and load?

3
  • 1
    what priorities are both of those enqueues hooked on?
    – Milo
    Aug 21, 2014 at 19:25
  • Those are the exact lines of code being called. I checked the wp_enqueue_style docs and I didnt see anything about priorities. How do I see its priority?
    – Jeff
    Aug 21, 2014 at 19:28
  • Oooh! You mean the hook's priority! Ah ha. Yeah, I reduced my hook's priority and that got it working! Make that an answer and I'll mark it.
    – Jeff
    Aug 21, 2014 at 19:42

1 Answer 1

6

Just use wp_register_style() and wp_enqueue_style() correctly.

Register your stylesheet on wp_loaded, enqueue it on wp_enqueue_scripts.

add_action( 'wp_loaded', function() {
    wp_register_style(
        'fontawesome',
        '//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css'
    );
});
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style( 'fontawesome' );
});

When someone tries to call wp_enqueue_style() with an URL, WordPress will try to register it automatically, doing the work of wp_register_style() behind the curtains. But that will fail if the $handle is already registered.

Always register scripts and stylesheet on wp_loaded. Never use wp_enqueue_style() and wp_enqueue_script() with the second parameter.

Do not fiddle with priorities here. The conflicting plugin might change its own priority, and you start running behind again.

3
  • Did you mean wp_enqueue_styles as opposed to wp_enqueue_scripts as the action? The former worked for me but the latter didn't. Regardless, thanks for the more informative answer than my question demanded!
    – Jeff
    Aug 21, 2014 at 21:09
  • @Jeff There is no action wp_enqueue_styles.
    – fuxia
    Aug 21, 2014 at 21:11
  • Weird that it is "working" then. Something else must be going on...
    – Jeff
    Aug 21, 2014 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.