L
L
Lexius2018-05-17 05:40:32
WordPress
Lexius, 2018-05-17 05:40:32

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;
}

By analogy, I added my own line, but after that, the settings in the admin panel of the qTranslate X plugin suddenly disappeared.
Can I connect Font Awesome through a ready-made plugin? Do I have a prejudice against plugins that they are overloaded with functions that are unnecessary for me and load the server in vain, or is this basically not the case?
Is there a simple and elegant solution to add just one line to functions.php?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Yanchevsky, 2018-05-17
@Lexius

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;
}

$handle is the value of the first parameter in the wp_enqueue_style function.

B
branky, 2018-08-24
@branky

You can conveniently edit the 'integrity' parameters with a custom function. I made an example that I am currently using in a working project, see my solution: How to connect Bootstrap to WordPress via CDN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question