style_loader_tag
style_loader_tag是官方的WordPress API,请参见文档:https : //developer.wordpress.org/reference/hooks/style_loader_tag/
apply_filters( 'style_loader_tag', $html, $handle, $href, $media )
筛选已排队样式的HTML链接标记。
首先将样式表放入队列,请参阅文档:https : //developer.wordpress.org/reference/functions/wp_enqueue_style/
wp_enqueue_style( 'font-awesome-5', 'https://use.fontawesome.com/releases/v5.5.0/css/all.css', array(), null );
该$handle
是'font-awesome-5'
我做的null
,这样就没有版本号。这样它将被缓存。
简单的str_replace
简单的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与wp_enqueue_script结合使用。
script_loader_tag API几乎相同,只有一些小差异,请参阅文档:https : //developer.wordpress.org/reference/hooks/script_loader_tag/
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( 'script_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都有经验。
UPDATE
@CKMacLeod感谢您的更新,这使事情变得清晰起来。我们大多在同一页面上。就像我说的,我是WordPress开发人员,如果您想在https://wordpress.org上发布主题和/或插件,您实际上会被迫遵守“ WordPress编码标准 ”,否则他们将拒绝您的主题和/或插件。
这就是为什么我鼓励开发人员使用“ WordPress方式”的原因。我知道有时候之间没有任何区别,但这也很方便。正如您自己说的那样,您可以下载Font Awesome并将其包含在主题和/或插件中,这样,您只需删除style_loader_tag函数并修改wp_enqueue_style函数。
最后一件事,在过去(5年前)中,某些缓存,组合和缩小插件不起作用,而且大多数时候,我会发现为什么制作主题的开发人员只是将主题放在首位, /或回应他们。大多数(大部分时间)还提供组合,最小化和延迟脚本执行的选项的缓存插件变得更聪明,并且在检测错误代码和解决它们方面变得更好。但这不是首选。这就是为什么我鼓励人们在编写代码时要牢记标准/惯例。
作为开发人员,您始终需要考虑人们可以使用您的代码做什么,并且要考虑兼容性。因此,不是采用简单的解决方案而是最佳的最佳解决方案。我希望我已经阐明了我的观点。
EDIT
@CKMacLeod感谢您的(技术性)辩论,我希望您意识到它的重要性(在您自己的开发中使用WordPress API)。同样,我一直在环顾四周,即使现在查看最受欢迎的minify插件的FAQ,我通常也会以一种或另一种方式看到这种情况,例如:
问题:为什么某些CSS和JS文件没有合并?
答:该插件仅处理使用官方WordPress API方法排队的JS和CSS文件-https:
//developer.wordpress.org/themes/basics/includes-css-javascript/-
以及来自同一域的文件(除非指定在设置上)。
请参阅常见问题解答:https://wordpress.org/plugins/fast-velocity-minify/