Created
March 31, 2015 22:36
-
-
Save LinzardMac/0f61aa9a037a63d53bdb to your computer and use it in GitHub Desktop.
Reusable meta box for WordPress - Grouped input fields
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
Works when plugged in to custom reusable metaboxes by TammyHart | |
This code takes the values of THREE input fields and saves them as one meta option value. This works particularly well for time inputs where the user enters the hour, minute, and am/pm and you want to store a unix timestamp OR a 24 hour time stamp | |
// settings for grouped input fields | |
array( | |
'label'=> 'Start Time', | |
'desc' => '', | |
'id' => $prefix.'startselect', | |
'type' => 'timegroup', | |
'options' => array( | |
'hour' => array( | |
'label'=> '', | |
'desc' => '', | |
'id' => $prefix.'hourselect', | |
'type' => 'hourselect', | |
'options' => getHourIntervals(), | |
), | |
'min' => array( | |
'label'=> '', | |
'desc' => '', | |
'id' => $prefix.'minselect', | |
'type' => 'minselect', | |
'options' => getMinIntervals(), | |
), | |
'meridiem' => array( | |
'label'=> '', | |
'desc' => '', | |
'id' => $prefix.'meridiemselect', | |
'type' => 'meridiemselect', | |
'options' => array( | |
'am' => array( | |
'label' => 'am', | |
'value' => 'am' | |
), | |
'pm' => array( | |
'label' => 'pm', | |
'value' => 'pm'), | |
) | |
) | |
) | |
), | |
// HTML form output for your group field type (specifically coded for hour, minute, meridien) This has conditional logic in it for taking the single value in the database and outputting the proper values into the form fields so that the form is autofilled with the existing values. | |
case 'timegroup': | |
echo '<div id="'.$field['id'].'" id="'.$field['id'].'">'; | |
foreach ($field['options'] as $option) { | |
echo '<select name="'.$field['id'].'['.$option['id'].']" id="'.$option['id'].'">'; | |
foreach($option['options'] as $the_option){ | |
$time = strtotime($meta); | |
$hour = date('h', $time); | |
$min = date('i', $time); | |
$meridiem = date('a', $time); | |
if($option['type'] == 'hourselect'){ | |
echo '<option', $hour == $the_option['value'] ? ' selected="selected"' : '', ' value="'.$the_option['value'].'">'.$the_option['label'].'</option>'; | |
}elseif($option['type'] == 'minselect') { | |
echo '<option', $min == $the_option['value'] ? ' selected="selected"' : '', ' value="'.$the_option['value'].'">'.$the_option['label'].'</option>'; | |
} | |
elseif($option['type'] == 'meridiemselect') { | |
echo '<option', $meridiem == $the_option['value'] ? ' selected="selected"' : '', ' value="'.$the_option['value'].'">'.$the_option['label'].'</option>'; | |
}else{ | |
echo '<option', $meridiem == $the_option['value'] ? ' selected="selected"' : '', ' value="'.$the_option['value'].'">'. | |
$the_option['label'].'</option>'; | |
} | |
} //end foreach options as the_option | |
echo '</select>'; | |
} //end foreach field as option | |
echo '</div>'; | |
break; | |
// Save these fields differently than other fields because we have to combine the values of the hour, minutes, and meridian into one update_post_meta() call. This could probably be made more effecient | |
//check if it's a grouped input field | |
if($fieldName == 'custom_startselect'){ | |
// get value of "hour" input | |
$hour = $_POST['custom_startselect']['custom_hourselect']; | |
//get value of "minput" input | |
$min = $_POST['custom_startselect']['custom_minselect']; | |
//get value of AM/PM | |
$meridien = $_POST['custom_startselect']['custom_meridiemselect']; | |
// group them togher so output is hour:minute am/pm | |
$group = $hour.':'.$min.$meridien; | |
//format is 24 hour time code | |
$time = date("H:i", strtotime($group)); | |
//update post meta field | |
update_post_meta($post_id, $fieldName , $time); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment