dtjpnd7517 2014-06-02 21:50
浏览 94
已采纳

将datepicker字段添加到Wordpress中的插件设置页面

I'm developing a simple plugin (my first WP plugin development) and I'm trying to add a datepicker field in the plugin settings page using this code:

add_settings_field('example_date_picker', 'Example Date Picker', 'pu_display_date_picker', 'ft_admin.php', '', array());
add_action('admin_enqueue_scripts', 'enqueue_date_picker');

function pu_display_date_picker($args)
{
    extract($args);
    echo '<input type="date" id="datepicker" name="example[datepicker]" value="" class="example-datepicker" />';
}

/**
 * Enqueue the date picker
 */
function enqueue_date_picker()
{
    wp_enqueue_script(
            'field-date-js', 'ft.js', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'), time(), true
    );

    wp_enqueue_style('jquery-ui-datepicker');
}

The code come from this post but I get this error:

Fatal error: Call to undefined function add_settings_field() in /var/www/html/arubaair/wp-content/plugins/frequent-traveler/frequent-traveler.php on line 41

And I don't know why. The plugin is installed and active and if I remove the code all works. What I'm doing wrong?

UPDATE

I change the original code to this one:

add_action('admin_enqueue_scripts', 'enqueue_date_picker');

function enqueue_date_picker()
{
    wp_enqueue_script(
            'field-date-js', 'ft.js', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'), time(), true
    );

    wp_enqueue_style('jquery-ui-datepicker');
}

The file ft.js is on js plugin directory. Then in the page where I build the form I have this line:

<input type="date" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="" class="datepicker" />

And ft.js contains this code:

jQuery(document).ready(function() {
    jQuery('.datepicker').datepicker();
});

And it's not working, I check the page source and scripts aren't loaded, why?

  • 写回答

2条回答 默认 最新

  • douyue9704 2014-06-11 20:28
    关注

    As your code is not compilable, I'll post a working example based on the skeleton of your sample code. Required: PHP 5.3, see Lambda functions:

    add_action( 'admin_menu', function()
    {
        $hook = add_menu_page( 
            'Date Pick', 
            'Date Pick', 
            'manage_options', 
            'sub-page-date-picker', 
            function() { 
                echo '<h1>Date Pick</h1>'; 
                echo '<input type="date" id="datepicker" name="example[datepicker]" value="" class="example-datepicker" />';
            }
        );
        # echo $hook; die(); // get the correct hook slug
    
        add_action( 'admin_enqueue_scripts', function( $hook )
        {
            if( 'toplevel_page_sub-page-date-picker' !== $hook )
                return;
    
            wp_enqueue_script(
                'field-date-js', 
                plugins_url( '/ft.js', __FILE__ ), 
                array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'), 
                time(), 
                true
            );
            wp_enqueue_style('jquery-ui-datepicker');
        });
    });  
    

    And ft.js with a small adjustment:

    jQuery(document).ready(function($) {
        $('.datepicker').datepicker();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)