How to dequeue a stylesheet by src, not handle?

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' );

Leave a Comment