How to add functions.php styles Font Awesome and others that use additional attributes (integrity, by adding crossorigin)?

Hello! I'm still very poorly versed in PHP and Wordpress features, so I decided to ask the experts. How scary to insert styles in old-fashioned header.php? Because without the crutches to insert a simple string like:
<link rel="stylesheet" href="" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqg0tfsghavtvlrw3tvkxwzh58n9jp" by adding crossorigin="anonymous">
in the code functions.php I have not found. Found here is the solution with arrays:
add_filter('script_loader_tag', 'add_attributes_to_script', 10, 3); 
add_attributes_to_script function( $tag, $handle, $src ) {

$scripts_to_load = array (

 (0) => Array
 ('name') => 'bootstrap_min_css',
 ('type') => '<link rel="stylesheet" href="', 
 ('integrity') => 'sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB',
 ('close') => ' type=" text css" media="all">'

 (1) => Array
 ('name') => 'popper_min_js',
 ('type') => '<script type="text/javascript" src="', 
 ('integrity') => 'sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjak/l8WvCWPIPm49',
 ('close') => '></script>'

 (2) => Array
 ('name') => 'bootstrap_min_js', 
 ('type') => '<script type=" text javascript" integrity="' . $scripts_to_load[$key]['integrity'] .'" by adding crossorigin="anonymous" ' . $scripts_to_load[$key]['close'] "\n"; } return $tag; }< code></pre><br>
by analogy added its own line, but then suddenly disappeared in the admin settings of the plugin qTranslate X.<br>
Can connect the Font Awesome via ready-made plugin? I have a prejudice against plugins, that they are overloaded with unnecessary functions for me and upload nothing to the server, or mostly it doesn't?<br>
There is generally a simple and elegant solution, just one line to add in functions.php?</script>
June 5th 19 at 21:40
2 answers
June 5th 19 at 21:42

I think you can try using filter style_loader_tag.

add_filter( 'style_loader_tag', 'add_attribute', 10, 2 );

function add_attribute($link, $handle) {
 if( $handle == 'fontawesome' ) {
 $link = str_replace( '/>', 'integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqg0tfsghavtvlrw3tvkxwzh58n9jp" by adding crossorigin="anonymous" />', $link );
 return $link;

$handle is the value of the first parameter in the function wp_enqueue_style.
Thank you very much, Dennis! It works! - nikita.Stracke commented on June 5th 19 at 21:45
June 5th 19 at 21:44
You can conveniently edit parameters 'integrity' using a custom function. I made an example, which now use working, see my solution: How to connect Bootstrap to WordPress via CDN

Find more questions by tags WordPress