我最初的问题在这里得到了回答:谷歌字体提供:没有‘访问-控制-允许-起源’头是存在于所请求的资源。
有没有办法将数据-noprefix属性添加到我的Google字体链接标签中?
我的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' );发布于 2015-01-27 20:00:57
这对我起了作用:
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;
}发布于 2018-11-24 11:21:05
style_loader_tag是官方的WordPress API,请参阅文档:标签/
apply_filters( 'style_loader_tag', $html, $handle, $href, $media )过滤已排队样式的HTML链接标记。
--首先,将样式表()排队
有关更多信息,请参见文档:风格/风格
function add_styles() {
// Example loading external stylesheet, could be used in both a theme and/or plugin
wp_enqueue_style( 'font-awesome-5', 'https://use.fontawesome.com/releases/v5.5.0/css/all.css', array(), null );
// Example theme
wp_enqueue_style( 'font-awesome-5', get_theme_file_uri( '/assets/css/fontawesome.min.css' ), array(), null );
// Example plugin
wp_enqueue_style( 'font-awesome-5', plugins_url( '/assets/css/fontawesome.min.css', __FILE__ ), array(), null );
};
add_action( 'wp_enqueue_scripts', 'add_styles' );$handle是'font-awesome-5'
我做null,这样就没有版本号了。这样它就会被缓存。
简单字符串替换
一个简单的str_replace就足以实现您想要的结果,参见下面的示例
function add_font_awesome_5_cdn_attributes( $html, $handle ) {
if ( 'font-awesome-5' === $handle ) {
return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
}
return $html;
}
add_filter( 'style_loader_tag', 'add_font_awesome_5_cdn_attributes', 10, 2 );添加不同的
下面的示例将不同的属性添加到(多个)不同的样式表中
function add_style_attributes( $html, $handle ) {
if ( 'font-awesome-5' === $handle ) {
return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
}
if ( 'another-style' === $handle ) {
return str_replace( "media='all'", "media='all' integrity='blajbsf' example", $html );
}
if ( 'bootstrap-css' === $handle ) {
return str_replace( "media='all'", "media='all' integrity='something-different' crossorigin='anonymous'", $html );
}
return $html;
}
add_filter( 'style_loader_tag', 'add_style_attributes', 10, 2 );向所有样式添加属性()
下面的示例将相同的属性添加到多个样式表中。
function add_attributes_to_all_styles( $html, $handle ) {
// add style handles to the array below
$styles = array(
'font-awesome-5',
'another-style',
'bootstrap-css'
);
foreach ( $styles as $style ) {
if ( $style === $handle ) {
return str_replace( "media='all'", "media='all' integrity='sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU' crossorigin='anonymous'", $html );
}
}
return $html;
}
add_filter( 'style_loader_tag', 'add_attributes_to_all_styles', 10, 2 );script_loader_tag
我还想解释一下script_loader_tag,因为这也很方便,但是这个API与脚本一起工作。
script_loader_tag API几乎相同,只是一些小的差异,请参阅文档:标签/。
apply_filters( 'script_loader_tag', $tag, $handle, $src )过滤队列脚本的HTML脚本标记。
下面是一个延迟单个脚本的示例
function add_defer_jquery( $tag, $handle ) {
if ( 'jquery' === $handle ) {
return str_replace( "src", "defer src", $tag );
}
return $tag;
}
add_filter( 'style_loader_tag', 'add_defer_jquery', 10, 2 );下面是一个延迟多个脚本的示例
function add_defer_attribute( $tag, $handle ) {
// add script handles to the array below
$scripts_to_defer = array(
'jquery',
'jquery-migrate',
'bootstrap-bundle-js'
);
foreach ( $scripts_to_defer as $defer_script ) {
if ( $defer_script === $handle ) {
return str_replace( "src", "defer src", $tag );
}
}
return $tag;
}
add_filter( 'script_loader_tag', 'add_defer_attribute', 10, 2 );所以我已经解释了API的style_loader_tag和script_loader_tag。我给出了两个API的一些例子,我希望这对很多人都有帮助。我对这两个API都有经验。
https://stackoverflow.com/questions/28158492
复制相似问题