Zorro Here 今天,当我想为排队脚本解决同样的问题时,我偶然发现了这个问题。我猜你真正想要的是noscript
在脚本之后,而不是样式之后,对吗
如果是,我将分享我解决问题的方法,因为它可能也会对您有所帮助。
使用noscript
遗憾的是,到目前为止,WordPress还没有一种特定于本机的方法来向任何使用本机wp\\u register\\u script/wp\\u enqueue\\u script functionsw排队的Javascript添加noscript元素
好消息是,你可以通过使用WordPress过滤器来解决这个问题。具体来说,通过使用script\\u loader\\u标记,如下面的示例所示。
/**
* @summary filters an enqueued script tag and adds a noscript element after it
*
* @description filters an enqueued script tag (identified by the $handle variable) and
* adds a noscript element after it. If there is also an inline script enqueued
* after $handled, adds the noscript element after it.
*
* @access public
* @param string $tag The tag string sent by `script_loader_tag` filter on WP_Scripts::do_item
* @param string $handle The script handle as sent by `script_loader_tag` filter on WP_Scripts::do_item
* @param string $src The script src as sent by `script_loader_tag` filter on WP_Scripts::do_item
* @return string $tag The filter $tag variable with the noscript element
*/
function add_noscript_filter($tag, $handle, $src){
// as this filter will run for every enqueued script
// we need to check if the handle is equals the script
// we want to filter. If yes, than adds the noscript element
if ( \'script-handle\' === $handle ){
$noscript = \'<noscript>\';
// you could get the inner content from other function
$noscript .= \'<p>this site demands javascript</p>\';
$noscript .= \'</noscript>\';
$tag = $tag . $noscript;
}
return $tag;
}
// adds the add_noscript_filter function to the script_loader_tag filters
// it must use 3 as the last parameter to make $tag, $handle, $src available
// to the filter function
add_filter(\'script_loader_tag\', \'add_noscript_filter\', 10, 3);
基本上,你必须附加
noscript
使用
add_noscript_filter
, 这给了你最后的机会
script
包含原始脚本和
wp_enqueue_script
以及添加的任何内联脚本
wp_add_inline_script
钩
使用noscript
对于样式,如果您真的需要将其用于样式,有style_loader_tag
以类似方式工作的过滤器
您可以这样使用它:
/**
* @summary filters an enqueued style tag and adds a noscript element after it
*
* @description filters an enqueued style tag (identified by the $handle variable) and
* adds a noscript element after it.
*
* @access public
* @param string $tag The tag string sent by `style_loader_tag` filter on WP_Styles::do_item
* @param string $handle The script handle as sent by `script_loader_tag` filter on WP_Styles::do_item
* @param string $href The style tag href parameter as sent by `script_loader_tag` filter on WP_Styles::do_item
* @param string $media The style tag media parameter as sent by `script_loader_tag` filter on WP_Styles::do_item
* @return string $tag The filter $tag variable with the noscript element
*/
function add_noscript_style_filter($tag, $handle, $href, $media){
// as this filter will run for every enqueued script
// we need to check if the handle is equals the script
// we want to filter. If yes, than adds the noscript element
if ( \'script-handle\' === $handle ){
$noscript = \'<noscript>\';
// you could get the inner content from other function
$noscript .= \'<p>this site demands javascript</p>\';
$noscript .= \'</noscript>\';
$tag = $tag . $noscript;
}
return $tag;
}
// adds the add_noscript_filter function to the style_loader_tag filters
// it must use 4 as the last parameter to make $tag, $handle, $href, $media available
// to the filter function
add_filter(\'style_loader_tag\', \'add_noscript_style_filter\', 10, 4);
参考文献: