3

My original question was answered here: Google Fonts giving: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is there a way to add the data-noprefix attribute to my Google Fonts link tag?

My functions.php looks like this:

wp_register_script( 'prefixfree', get_template_directory_uri().'/js/prefixfree.min.js', array( 'jquery' ) );
wp_enqueue_script( 'prefixfree' );

wp_register_style( 'google-fonts', 'http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,600,600italic,700,700italic', '', '', 'all' );
wp_enqueue_style( 'google-fonts' );

Answer, with the help of Birgire:

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

function add_noprefix_attribute($link, $handle) {
    if( $handle === 'google-fonts' ) {
        $link = str_replace( '/>', 'data-noprefix />', $link );
    }
    return $link;
}
4

Looking at the WP_Style class, we find the style_loader_tag filter, that might be useful.

Try for example:

add_filter( 'style_loader_tag', function( $link, $handle )
{
    if( 'google-fonts' === $handle )
    {
        $link = str_replace( '/>', ' data-noprefix />', $link );
    }
    return $link;
}, 10, 2 );
  • What is the 10, 2 for? – user8737780 Jan 27 '15 at 19:54
  • 10 stands for the filter priority and 2 is the number of input arguments we want to use in the callback function. The default is 10, 1. – birgire Jan 27 '15 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.