向通过 wp_register_style 生成的链接标签添加属性?

use*_*780 5 css php wordpress prefixfree

我的原始问题在这里得到了回答:Google Fonts give: No 'Access-Control-Allow-Origin' header is present on the required resource。

有没有办法将 data-noprefix 属性添加到我的 Google Fonts 链接标签?

我的functions.php 看起来像这样:

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' );
Run Code Online (Sandbox Code Playgroud)

use*_*780 4

这对我有用:

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;
}
Run Code Online (Sandbox Code Playgroud)