Search code examples
phpcsswordpressprefixfree

Add attribute to link tag that's generated through wp_register_style?


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' );

Solution

  • This worked for me:

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