dongpu3347 2019-07-04 17:49
浏览 87

无法让jquery datepicker在Wordpress插件中运行

I'm new to jquery and php and trying to create my first Wordpress Plugin. The plugin simply adds a metabox to posts which has a date field on the backend.

Metabox and field all added ok and appear when I go to create or edit a post.

However, I can't get the datepicker to work. Nothing happens when you click on the input box, the date selector does not show. Although the input box will only let me input numbers not letters.

The plugin is a single page of php code.

The top of my plugin file attempts to load the 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' );

Then this is the input and jquery in my form (lower down on the plugin file):

<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> 

Can anyone help advise where I am going wrong with the datepicker? Thank you!

UPDATE

Thanks for the comments, I have discovered that if I instal the "Classic Editor" plugin, my code works. But when that is not activated and using Gutenberg it does not work.

The error is (without sharing the site url): GET https://xxxx.co.uk/wp-content/themes/publisher/gutenberg-7.6.2.min.css?ver=7.6.2 net::ERR_ABORTED 404 (Not Found)

This error is still there even when my plugin is disabled.

However, if I activate the Twenty Nineteen theme there is no error but the date field in my plugin still does not work.

So clearly gutenberg does not like something in my code...

  • 写回答

1条回答 默认 最新

  • dpxo13079 2019-07-05 08:08
    关注

    I would make sure to register the whole jquery-ui package from the remote official source in the functions.php of your current theme, like this:

    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' );
    

    Note: if you want to use your calendar in the frontend, please make sure to use the function add_action( 'wp_enqueue_scripts', 'xxx' ); instead of add_action( 'admin_enqueue_scripts', 'xxx' ); which is only for the admin side.


    UPDATE

    You might also try to disable the default jQuery or jQuery-ui to see if that helps:

    //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' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了