0

I'm trying to develop a Wordpress plugin that will dequeue/deregister Font Awesome stylesheets added to a theme by certain plugins that users may install, if the theme already make use of Font Awesome.

In other words: if an administrator uses a theme that has Font Awesome font-awesome.css (or font-awesome.min.css) and he uses 2 plugins that make use of Font Awesome too, the administrator will ended up with 3 font-awesome.css (or font-awesome.min.css) stylesheets loaded.

Is there a way to check if the Font Awesome stylesheet is already loaded by the theme and if so, automatically dequeue/deregister all plugins Font Awesome stylesheets, before the theme enqueue it?

The solution I have in mind is something like this (pseudo code):

add_action( 'wp_enqueue_scripts', 'no_more_fontawesome', 999 );
function no_more_fontawesome() {
    if ( src_contain ('font-awesome.css') || src_contain('font-awesome.min.css') ) {
        wp_dequeue_style( that has font-awesome.css or font-awesome.min.css in src);
    }

A solution might be a regex check in src for the expressions font-awesome.css and font-awesome.min.css but how to achieve this?

Any help will be greatly appreciated!

  • I think you are wrong. If all plugins and themes that use Font Awsome are enqueuing the styles properly, Wordpress will enqueue only one file and will take care of dependencies. The registration and enqueue system in Wordpress is just great. – cybmeta May 28 '14 at 7:52
  • @cybmeta in most cases plugin developers tend to use different names as a handle or prefix their handle with plugin prefix e.g mp_font-awesome, fontawesome, font-awesome etc.. – Knott May 28 '14 at 7:58
  • @Pieter Goosen They are totally different questions: one about dequeue styles based on src and another based of known handle. Are you serious? – Knott May 28 '14 at 8:29
  • My opinion, still the same question as both invlove the same exact stylesheets – Pieter Goosen May 28 '14 at 8:34
2

I think you are talking about a rare scenario. "font-awsome" is the expected name that all plugins and themes should be using as handle. If the developer has not changed the source code of font awsome CSS file, there is no reason to change the name of the handle. Anyway, you can check the global $wp_styles object and make a regex in the src value of each enqueued style:

function cybmeta_dequeue_fontawsome() {

    global $wp_styles;
    // we use preg_match to find only the following patterns as exact matches, to prevent other stylesheets that contain font-awesome expression to be also dequeued
    $patterns = array(
        'font-awesome.css',
        'font-awesome.min.css'
        );

    $regex = '/(' .implode('|', $patterns) .')/i';

    foreach( $wp_styles -> registered as $registered ) {

        if( preg_match( $regex, $registered->src) ) {
            wp_dequeue_style( $registered->handle );
            // FA was dequeued, so here we need to enqueue it again from the location we want (cdn in our code)
            wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css' );
        }
    }

}
add_action( 'wp_enqueue_scripts', 'cybmeta_dequeue_fontawsome' );
  • Thanks @cybmeta, your propossed code is work great but it dequeue the Font Awesome loadded by the theme too. The challange here is to check first if the Font Awesome stylesheet is already loaded by the theme, and only then dequeue extra stylesheets added by plugins. So, could you improve your answer? However, I will accept your answer since it provides a simple and clean solution than I expected. – Knott May 28 '14 at 9:48
  • about handle's name, be my guest to search for Font Awesome in WP repo and you will be surprised to find that any of the top 10 plugins haven't the same handle name. They called them as mp_font-awesome, fontawesome, font-awesome, etc.. – Knott May 28 '14 at 9:49
  • As I said, I think that changing the handle name for third party libraries is a bad thing and there is no reason to do it. You can see how doing that cause problems that you are trying to solve. Developers should follow naming standards. – cybmeta May 28 '14 at 12:20
  • So need to dequeue all font-awsome except from the theme? From a theme in general or from a specific theme? See edited answer. – cybmeta May 28 '14 at 12:26
  • you're right but we can't force plugin developers to follows the best practices or WP standards. Btw, can you improve the preg_match snippet to search only for font-awesome.css or font-awesome.min.css (exact matches) since several plugins load extra styesheets like font-awesome-social or font-awesome-ext.css? I don't want these stylesheets to be dequeued too. Thank you! – Knott May 28 '14 at 12:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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