如果我注册一个脚本或样式(使用 wp_register_script()
或 wp_register_style()
),有没有办法可以获取该脚本/样式的 URL?
(如果您一定要知道为什么,我正在尝试将这些 URL 放入另一个生成预取链接标记的函数中,以便我可以预取某些脚本/样式以提高我网站的性能。)
最佳答案
以防万一有人还在寻找这个:
<?php
function invdr_get_script_uri_by_handler( $handler ){
//Get an instance of WP_Scripts or create new;
$wp_scripts = wp_scripts();
//Get the script by registered handler name
$script = $wp_scripts->registered[ $handler ];
if ( file_exists( ABSPATH . $script->src ) ){
return ABSPATH . $script->src;
}
return false;
}
add_action( 'wp_enqueue_scripts', 'invdr_get_script_uri_by_handler', PHP_INT_MAX );
在 wordpress 5.0 中测试
关于php - wordpress 获取注册脚本的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341380/