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:
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();
});
For loading bellows script & style add bellows code on theme functions.php file.
Script for front-end use
function add_e2_date_picker(){
//jQuery UI date picker file
wp_enqueue_script('jquery-ui-datepicker');
//jQuery UI theme css file
wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
}
add_action('wp_enqueue_scripts', 'add_e2_date_picker');
Script for back-end use
function add_e2_date_picker(){
//jQuery UI date picker file
wp_enqueue_script('jquery-ui-datepicker');
//jQuery UI theme css file
wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
}
add_action('admin_enqueue_scripts', 'add_e2_date_picker');
Just put this code also funtions.php file or bellow those code.
function register_datepiker_submenu() {
add_submenu_page( 'options-general.php', 'Date Picker', 'Date Picker', 'manage_options', 'date-picker', 'datepiker_submenu_callback' );
}
function datepiker_submenu_callback() { ?>
<div class="wrap">
<input type="text" class="datepicker" name="datepicker" value=""/>
</div>
<script>
jQuery(function() {
jQuery( ".datepicker" ).datepicker({
dateFormat : "dd-mm-yy"
});
});
</script>
<?php }
add_action('admin_menu', 'register_datepiker_submenu');
?>
You’ll got a date picker on Admin Menu->Settigns->Date Picker. See more details Add a jQuery DatePicker to WordPress Theme or Plugin.