php - wordpress 获取注册脚本的 url

标签 php wordpress

如果我注册一个脚本或样式(使用 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/

相关文章:

html - 如何基于 acf 设置简码样式

PHP- 需要一个 cron 用于用户注册时的后台处理...(或 fork 进程)

php - 有条件地自定义 WooCommerce 结账字段

php - 可以在没有 jquery 的情况下索引组合框吗?

php - 在 Laravel 中通过 ajax 加载更 Eloquent hasMany 关系查询?

php - 将 IE8 用户重定向到另一个页面

wordpress - WP REST API 自定义 POST 端点不工作,404 错误

database - 将 CSV 文件加载到数据库表 Wordpress

php - 使用 PHPExcel 函数将日期和日期时间写入 Excel 中作为文本

php - nginx php-fpm 配置达到 child 限制