Created
March 4, 2014 14:57
-
-
Save gicolek/9347942 to your computer and use it in GitHub Desktop.
WordPress Head Inline js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_head', 'gf_trigger_picker' ); | |
/** | |
* Inject js code into the head, used with ACF | |
* @hook wp_head | |
*/ | |
function gf_trigger_picker() { | |
// get the global post values | |
global $post; | |
$id = $post->ID; | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
<?php if ( get_field( 'loc_date', $id ) ): ?> | |
// this function is used by the jQuery DatePicker API http://api.jqueryui.com/datepicker/ | |
availableDates = function(date) { | |
// create the proper format for comparison (equal to the value of ACF custom field) | |
var dmy = date.getFullYear() + '' + ('0' + (date.getMonth() + 1)).slice(-2) + '' + ('0' + date.getDate()).slice(-2); | |
// check what date is available getting the value from the custom field | |
if (dmy === '<?php echo get_field( 'loc_date', $id ); ?>') { | |
return [true, "available", "Available"]; | |
} else { | |
return [false, "", "unAvailable"]; | |
} | |
}; | |
// inititate the datepicker | |
$(".datepicker").datepicker( | |
{ | |
// lets make sure that the dates are limited to the values of our ACF field | |
beforeShowDay: availableDates | |
} | |
); | |
<?php endif; ?> | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment