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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用