我最初的问题在这里得到了回答:Google Fonts giving: No \'Access-Control-Allow-Origin\' header is present on the requested resource.
有没有办法将data noprefix属性添加到我的谷歌字体链接标签中?
我的职能。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\' );
在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;
}
最合适的回答,由SO网友:birgire 整理而成
查看WP_Style
同学们,我们发现style_loader_tag
过滤器,这可能很有用。
请尝试以下示例:
add_filter( \'style_loader_tag\', function( $link, $handle )
{
if( \'google-fonts\' === $handle )
{
$link = str_replace( \'/>\', \' data-noprefix />\', $link );
}
return $link;
}, 10, 2 );