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?
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:
And
ft.js
with a small adjustment:For loading bellows script & style add bellows code on theme functions.php file.
Script for front-end use
Script for back-end use
Just put this code also funtions.php file or bellow those code.
Youâll got a date picker on Admin Menu->Settigns->Date Picker. See more details Add a jQuery DatePicker to WordPress Theme or Plugin.