How to add extra attribute to stylesheet link?

We can use style_loader_tag filter to filter the link that is being output.

Here is the filter:

$tag = apply_filters( 'style_loader_tag', "<link rel="$rel" id='$handle-css' $title href="https://wordpress.stackexchange.com/questions/231597/$href" type="text/css" media="$media" />\n", $handle, $href, $media);

Here the link $handle for which you want to add attribute is font-awesome so if the handle, so you can replace font-awesome-css with extra info.

add_filter('style_loader_tag', 'wpse_231597_style_loader_tag');

function wpse_231597_style_loader_tag($tag){

    $tag = preg_replace("/id='font-awesome-css"https://wordpress.stackexchange.com/", "id='font-awesome-css' online=\"if(media!='all')media="all"\"", $tag);

    return $tag;
}

Leave a Comment