2012-01-11 52 views
0

我正在尝试添加一个日期选择器到wordpress中的自定义元框。如果我入队,像这样麻烦添加jQuery UI到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')日期选择器()。不是一个功能。当我检查处理后的源代码时,我发现默认情况下会加载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 /脚本()函数或与ADD_ACTION钩出了问题?我是wordpress的新手,所以我不确定如何解决这个问题。我已经做了谷歌搜索,我去的代码看起来像我的。此外,如果你想知道文件的路径是正确的。

任何人都可以想到解决我的问题?只是回应剧本和风格,还是应该让他们入选?

谢谢!

编辑:

file_exists($ jsFile)返回false。如果我删除条件检查文件的代码工作..但为什么?你还能怎样检查一个使用以http://开头的URL而不是本地文件路径的文件?

回答

0

如果你想获得你的主题本地路径,你可以使用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函数 。

+0

仍然无法使用。我试着fopen没有成功,get_theme_root()从我的共享主机的根目录返回文件路径,而不是我的域指针的根目录。 – 2012-01-11 23:15:24

+0

'get_template_directory()'而不是'get_theme_root()'怎么办?我已经尝试了两种解决方案(file_exists和fopen)安装WordPress,他们似乎正在为我工​​作。 – chrisn 2012-01-12 02:16:21