【问题标题】:trouble adding jQuery UI to Wordpress admin page将 jQuery UI 添加到 Wordpress 管理页面时遇到问题
【发布时间】:2012-01-11 01:12:19
【问题描述】:

我正在尝试将日期选择器添加到 wordpress 中的自定义元框。如果我像这样将脚本排入队列

// add stylesheets for date picker
add_action('admin_print_styles', 'add_datepicker_styles');
function add_datepicker_styles() {
    global $post;
    $styleFile = get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
    if(file_exists($styleFile) && $post->post_type == 'show') {
        wp_enqueue_style("datepicker-css", $styleFile);
    }
}
// add javascript for date picker
add_action('admin_print_scripts', 'add_datepicker_js');
function add_datepicker_js() {
    global $post;
    $jsFile = get_bloginfo("template_url").'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js';
    if(file_exists($jsFile) && $post->post_type == 'show') {
        wp_enqueue_script("datepicker-js", $jsFile);
    }
}
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
    global $post;
    if($post->post_type == 'show') {
        echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
    }
}

我收到一个错误,即 jQuery('#show_date').datepicker();不是函数。当我检查处理后的源代码时,我看到默认情况下会加载 jQuery,但根本没有加载样式和 jQuery UI 脚本。附加到 admin_head 钩子的代码加载正常,如果我在此函数中回显脚本和样式,它会按照我想要的方式工作。

// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
    global $post;
    if($post->post_type == 'show') {
        echo "<link rel='stylesheet' type='text/css' href='".get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css'."' />";
        echo "<script type='text/javascript' src='".get_bloginfo('template_url').'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js'."'></script>";
        echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
    }
}

那么是 wp_enqueue_style/script() 函数还是 add_action 钩子有问题?我是 wordpress 的新手,所以我不确定如何解决这个问题。我已经进行了谷歌搜索,无论我走到哪里,代码看起来都像我的一样。此外,如果您想知道文件的路径是否正确。

谁能想到我的问题的解决方案?只是回显脚本和样式是错误的,还是应该将它们排入队列?

谢谢!

编辑:

file_exists($jsFile) 返回 false。如果我删除了对文件的条件检查,代码就可以工作了。但是为什么呢?您如何使用以 http:// 开头的 url 而不是本地文件路径来检查存在的文件?

【问题讨论】:

    标签: wordpress jquery-ui datepicker


    【解决方案1】:

    如果你想为你的主题获取一个本地路径,你可以使用get_theme_root():

    $styleFile = get_theme_root().'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
        if(file_exists($styleFile) && $post->post_type == 'show') {
    

    来自php.net/file_exists

    这里的代码是为了检查文件是否存在于 另一台服务器:

    <?php function fileExists($path){
        return (@fopen($path,"r")==true); } ?>
    

    不幸的是,file_exists 无法访问远程服务器,所以我使用了 fopen 函数。

    【讨论】:

    • 还是不行。我尝试 fopen 没有成功,get_theme_root() 从我的共享主机的根目录返回文件路径,而不是我的域指针的根目录。
    • get_template_directory() 代替 get_theme_root() 怎么样?我在安装 WordPress 时尝试了两种解决方案(file_exists 和 fopen),它们似乎对我有用。
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2010-11-02
    • 2014-03-02
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多