Skip to content

Instantly share code, notes, and snippets.

@icetee
Last active September 30, 2017 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icetee/946683aca3f0149aba5c56d14ded2897 to your computer and use it in GitHub Desktop.
Save icetee/946683aca3f0149aba5c56d14ded2897 to your computer and use it in GitHub Desktop.
WordPress datepicker adjustable filtered days. Example: https://jsfiddle.net/icetee/by9aa4pt/
// @icetee - https://gist.github.com/icetee/946683aca3f0149aba5c56d14ded2897
(function($, opt) {
var picker = function(opt) {
var self = this;
self.opt = opt;
var $picker = $(self.opt.selector);
if (!$picker) return;
var attr = $.extend({
beforeShowDay: function(date) {
if (self.opt.showDays.indexOf(date.getDay()) > -1) {
return [true, "", self.opt.available];
}
return [false, "", self.opt.unavailable];
}
}, self.opt.datepicker || {});
$picker.datepicker(attr);
};
picker(opt);
})(jQuery, wmDatePickerOptions);
/*
* Usage: [wm_datepicker name="datetime-735" class="wmdatepicker" showdays="1, 2" available="Available" unavailable="unAvailable" showdays="1, 2" mindate="0" maxdate="1M" dateformat="yy.mm.dd"]
* @icetee - https://gist.github.com/icetee/946683aca3f0149aba5c56d14ded2897
*/
function wm_datepicker($atts) {
$atts = shortcode_atts([
'name' => 'datetime-' . rand(1, 99999999),
'type' => 'text',
'value' => '',
'class' => 'wmdatepicker',
'selector' => '.wmdatepicker',
'mindate' => -0,
'maxdate' => '1M',
'dateformat' => 'yy.mm.dd',
'showdays' => [4, 5, 6],
'available' => 'Available',
'unavailable' => 'unAvailable',
], $atts);
$pickerOptions = json_encode([
'selector' => $atts['selector'],
'datepicker' => [
'minDate' => $atts['mindate'],
'maxDate' => $atts['maxdate'],
'dateFormat' => $atts['dateformat'],
],
'showDays' => array_map('intval', explode(', ', $atts['showdays'])),
'available' => $atts['available'],
'unavailable' => $atts['unavailable'],
]);
echo '<script type="text/javascript">var wmDatePickerOptions='.$pickerOptions.';</script>';
return '<input id="'. $atts["id"] .'" name="'. $atts["name"] .'" type="'. $atts["type"] .'" value="'. $atts["value"] .'" class="'. $atts["class"] .'" />';
}
add_shortcode('wm_datepicker', 'wm_datepicker');
function check_shortcode_existence() {
global $post;
if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'wm_datepicker') && !is_admin()) {
wp_register_script('wm_datepicker', get_theme_file_uri('/assets/js/picker.js', __FILE__), array('jquery', 'jquery-ui-datepicker'), '1.0', TRUE);
wp_enqueue_script('wm_datepicker');
$wp_scripts = wp_scripts();
wp_enqueue_style(
'plugin_name-admin-ui-css',
'http://ajax.googleapis.com/ajax/libs/jqueryui/' . $wp_scripts->registered['jquery-ui-core']->ver . '/themes/smoothness/jquery-ui.css',
FALSE,
PLUGIN_VERSION,
FALSE
);
}
}
add_action('wp_enqueue_scripts', 'check_shortcode_existence');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment