Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active June 26, 2018 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisblakley/15848f01c432a5cee7a0 to your computer and use it in GitHub Desktop.
Save chrisblakley/15848f01c432a5cee7a0 to your computer and use it in GitHub Desktop.
Business Hours Custom Field Group without ACF
<?php
//Add Open/Closed info to location listing columns
add_filter('manage_edit-location_columns', 'schc_location_hours_columns_head');
function schc_location_hours_columns_head($defaults){
$defaults['openclosed'] = 'Open/Closed';
return $defaults;
}
add_action('manage_location_posts_custom_column', 'schc_location_hours_columns_content', 15, 3);
function schc_location_hours_columns_content($column_name, $id){
if ( $column_name == 'openclosed' ){
$date = time();
$today = strtolower(date('l', $date));
$locationHours = array();
foreach ( array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $weekday ){
$locationHours[$weekday] = array(
'enabled' => get_post_meta($id, 'schc_location_hours_' . $weekday . '_enabled', true),
'open' => get_post_meta($id, 'schc_location_hours_' . $weekday . '_open', true),
'close' => get_post_meta($id, 'schc_location_hours_' . $weekday . '_close', true)
);
}
if ( get_post_meta($id, 'schc_location_hours_daysoff', true) ){
$days_off = explode(', ', get_post_meta($id, 'schc_location_hours_daysoff', true));
} else {
$days_off = explode(', ', get_option('nebula_business_hours_closed'));
}
if ( !empty(array_filter($days_off)) ){
foreach ( $days_off as $key => $day_off ){
$days_off[$key] = strtotime($day_off . ' ' . date('Y', $date));
if ( date('N', $days_off[$key]) == 6 ){ //If the date is a Saturday
$days_off[$key] = strtotime(date('F j, Y', $days_off[$key]) . ' -1 day');
} elseif ( date('N', $days_off[$key]) == 7 ){ //If the date is a Sunday
$days_off[$key] = strtotime(date('F j, Y', $days_off[$key]) . ' +1 day');
}
if ( date('Ymd', $days_off[$key]) == date('Ymd', $date) ){
echo '<span style="color: #601a1a;">Off Today</span>';
return;
}
}
}
if ( $locationHours[$today]['enabled'] == '1' ){ //If the Nebula Options checkmark is checked for this day of the week.
$openToday = date('Gi', strtotime($locationHours[$today]['open']));
$closeToday = date('Gi', strtotime($locationHours[$today]['close']));
if ( date('Gi', $date) >= $openToday && date('Gi', $date) <= $closeToday ){
echo '<span style="font-weight: bold; color: #58c026;">Open Now</span>';
} else {
echo '<span style="color: #ca3838;">Closed</span>';
}
} else {
echo '<span style="color: #962828;">Closed Today</span>';
}
}
}
?>
<?php foreach ( array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $weekday ): ?>
<?php if ( get_post_meta(get_the_id(), 'schc_location_hours_' . $weekday . '_enabled', true) && get_post_meta(get_the_id(), 'schc_location_hours_' . $weekday . '_open', true) != '' && get_post_meta(get_the_id(), 'schc_location_hours_' . $weekday . '_close', true) != '' ): ?>
<meta property="business:hours:day" content="<?php echo $weekday; ?>" />
<meta property="business:hours:start" content="<?php echo get_post_meta(get_the_id(), 'schc_location_hours_' . $weekday . '_open', true); ?>" />
<meta property="business:hours:end" content="<?php echo get_post_meta(get_the_id(), 'schc_location_hours_' . $weekday . '_close', true); ?>" />
<?php endif; ?>
<?php endforeach; ?>
<?php
//Location Hours Metabox and Custom Fields
add_action('load-post.php', 'slsp_post_meta_boxes_setup');
add_action('load-post-new.php', 'slsp_post_meta_boxes_setup');
function slsp_post_meta_boxes_setup(){
add_action('add_meta_boxes', 'slsp_add_post_meta_boxes');
add_action('save_post', 'slsp_save_post_class_meta', 10, 2);
}
function slsp_add_post_meta_boxes(){
add_meta_box('slsp-location-hours', 'Location Hours', 'slsp_location_hours_meta_box', 'page', 'side', 'default');
}
function slsp_location_hours_meta_box($object, $box){
wp_nonce_field(basename(__FILE__), 'slsp_location_hours_nonce');
?>
<div>
<p style="font-size: 12px; color: #444;">Times should be in the format "5:30 pm" or "17:30".</p>
<?php
$weekdays = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
foreach ( $weekdays as $weekday ):
?>
<div class="businessday">
<input type="checkbox" name="slsp_location_hours_<?php echo $weekday; ?>_enabled" value="1" <?php checked('1', get_post_meta($object->ID, 'slsp_location_hours_' . $weekday . '_enabled', true)); ?> /> <span style="display: inline-block; width: 75px;"><?php echo ucfirst($weekday); ?>:</span> <input class="business-hour" type="text" name="slsp_location_hours_<?php echo $weekday; ?>_open" value="<?php echo get_post_meta($object->ID, 'slsp_location_hours_' . $weekday . '_open', true); ?>" style="width: 65px;" /> &ndash; <input class="business-hour" type="text" name="slsp_location_hours_<?php echo $weekday; ?>_close" value="<?php echo get_post_meta($object->ID, 'slsp_location_hours_' . $weekday . '_close', true); ?>" style="width: 65px;" />
</div>
<?php endforeach; ?>
</div>
<?php }
function slsp_save_post_class_meta($post_id, $post){
if ( !isset($_POST['slsp_location_hours_nonce']) || !wp_verify_nonce($_POST['slsp_location_hours_nonce'], basename(__FILE__)) ){
return $post_id;
}
$post_type = get_post_type_object($post->post_type); //Get the post type object.
if ( !current_user_can($post_type->cap->edit_post, $post_id) ){ //Check if the current user has permission to edit the post.
return $post_id;
}
$weekdays = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
$options = array('enabled', 'open', 'close');
foreach ( $weekdays as $weekday ){
foreach ( $options as $option ){
$new_meta_value = sanitize_text_field($_POST['slsp_location_hours_' . $weekday . '_' . $option]); //Get the posted data and sanitize it if needed.
$meta_value = get_post_meta($post_id, 'slsp_location_hours_' . $weekday . '_' . $option, true); //Get the meta value of the custom field key.
if ( $new_meta_value && $meta_value == '' ){ //If a new meta value was added and there was no previous value, add it.
add_post_meta($post_id, 'slsp_location_hours_' . $weekday . '_' . $option, $new_meta_value, true);
} elseif ( $new_meta_value && $meta_value != $new_meta_value ){ //If the new meta value does not match the old value, update it.
update_post_meta($post_id, 'slsp_location_hours_' . $weekday . '_' . $option, $new_meta_value);
} elseif ( $new_meta_value == '' && $meta_value ){ //If there is no new meta value but an old value exists, delete it.
delete_post_meta($post_id, 'slsp_location_hours_' . $weekday . '_' . $option, $meta_value);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment