我是jquery和php的新手,正在尝试创建我的第一个Wordpress插件。 该插件仅向帖子中添加了一个metabox,该postbox在后端具有日期字段。
metabox和字段都添加确定,并在我创建或编辑帖子时出现。
但是,我无法使日期选择器正常工作。当您单击输入框时,没有任何反应,日期选择器不显示。尽管输入框只会让我输入数字,而不是字母。
该插件是php代码的一页。
我的插件文件顶部尝试加载jquery:
function ca_load_jquery_datepicker() {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css');
}
add_action( 'admin_enqueue_scripts', 'ca_load_jquery_datepicker' );
然后这就是我表单中的输入和jquery(在插件文件中的下方):
<input type="text" class="date" name="ca_expiry_date" value="">
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.date').datepicker({
dateFormat : 'yy-mm-dd'
});
});
</script>
任何人都可以帮助告知我日期选择器出了什么问题吗? 谢谢!
更新
感谢您的评论,我发现如果安装“经典编辑器”插件,我的代码可以正常工作。但是,如果未激活该功能并使用Gutenberg,则无法使用。
错误是(不共享网站网址): GET https://xxxx.co.uk/wp-content/themes/publisher/gutenberg-7.6.2.min.css?ver=7.6.2 net :: ERR_ABORTED 404(未找到)
即使我的插件被禁用,此错误仍然存在。
但是,如果我激活了“二十十九”主题,则没有错误,但是插件中的日期字段仍然无法工作。
很明显,古腾堡不喜欢我的代码中的某些东西...
答案 0 :(得分:0)
我将确保在您当前主题的functions.php
中注册来自远程官方源的整个jquery-ui软件包,如下所示:
function ca_load_jquery_ui() {
// first, register the style from the remote official source
wp_register_style('jqueryuicss', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css', array('jquery-ui-styles'), '1.12.1');
wp_enqueue_style('jqueryuicss');
// then, register the core file from the remote official source, in footer
wp_register_script('jqueryui', '//code.jquery.com/ui/1.12.1/jquery-ui.min.js', array('jquery-ui'), '1.12.1', true);
wp_enqueue_script('jqueryui');
}
add_action( 'wp_enqueue_scripts', 'ca_load_jquery_ui' );
注意::如果您想在前端使用日历,请确保使用功能add_action( 'wp_enqueue_scripts', 'xxx' );
代替add_action( 'admin_enqueue_scripts', 'xxx' );
,后者仅适用于管理员。
更新
您还可以尝试禁用默认的jQuery或jQuery-ui,以查看是否有帮助:
//remove the default jQuery script
function remove_default_jquery(&$scripts){
if(is_admin()){
// try this...
$scripts->remove('jquery');
// or...
$scripts->remove('jquery-ui');
}
}
add_filter( 'wp_default_scripts', 'remove_default_jquery' );