Answer the question
In order to leave comments, you need to log in
How to properly add Font Awesome styles and others that use additional attributes (integrity, crossorigin) to functions.php?
Hello! I'm still very poorly versed in the PHP language and Wordpress features, so I decided to ask the experts a question. How scary is it to put the old fashioned styles in header.php? Because without crutches, insert a simple line like:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
I did not find it in the functions.php code. I found this solution with arrays:add_filter('script_loader_tag', 'add_attributes_to_script', 10, 3);
function add_attributes_to_script( $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" src="',
('integrity') => 'sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T',
('close') => '></script>'
)
);
$key = array_search($handle, array_column($scripts_to_load, 'name'));
if ( FALSE !== $key){
$tag = $scripts_to_load[$key]['type'] . esc_url($src) . '" integrity="' . $scripts_to_load[$key]['integrity'] .'" crossorigin="anonymous"' . $scripts_to_load[$key]['close'] . "\n";
}
return $tag;
}
Answer the question
In order to leave comments, you need to log in
Hello.
I think you can try using the style_loader_tag filter.
add_filter( 'style_loader_tag', 'add_attribute', 10, 2 );
function add_attribute($link, $handle) {
if( $handle == 'fontawesome' ) {
$link = str_replace( '/>', 'integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous" />', $link );
}
return $link;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question