php 将async或defer属性添加到排队的脚本中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将async或defer属性添加到排队的脚本中相关的知识,希望对你有一定的参考价值。

/**
 * Adds 'async' or 'defer' attributes to certain script elements.
 *
 * @param string $tag    The base HTML markup
 * @param string $handle The script's handle (first parameter in wp_enqueue_script)
 * @param string $src    The script's source (second parameter in wp_enqueue_script)
 *
 * @return string        The complete HTML markup for the script tag.
 */
function pixel_designs_script_special_attributes ( $tag, $handle, $src ) {
	// For scripts that require the 'async' attribute, include their handles in this array.
	$async = [
		'google-maps-js',
	];
	
	// For scripts that require the 'defer' attribute, include their handles in this array.
	$defer = [
		'google-maps-js',
	];
	
	if ( in_array( $handle, $async ) ) {
		$tag = str_replace( 'src', 'async src', $tag );
	}
	if ( in_array( $handle, $defer ) ) {
		$tag = str_replace( 'src', 'defer src', $tag );
	}
	
	return $tag;
}
add_filter( 'script_loader_tag', 'pixel_designs_script_special_attributes', 10, 3 );

以上是关于php 将async或defer属性添加到排队的脚本中的主要内容,如果未能解决你的问题,请参考以下文章

php alterar regras grid flatsome - 网格系统短代码

JQuery 的 deferred . promise对象

php 使用HTML5 defer属性推迟jQuery解析

php Gofas Gerencianet Boleto para WHMCS - https://gofas.net/foruns/topico/alterar-link-do-whmcs/

使用functions.php将Bootstrap排队到Wordpress

在 body 标记之前使用带有脚本的 'defer' 属性