class wpturbo-custom-meta-box{
private $display_locations = array(
);
private $fields = array(
);
public function __construct(){
add_action( 'add_meta_boxes', array( $this, 'wpturbo_register_meta_boxes' ) );
add_action( 'save_post', array( $this, 'wpturbo_save_meta_box_fields' ) );
}
public function wpturbo_register_meta_boxes(){
foreach ( $this->display_locations as $location ) {
add_meta_box(
'wpturbo-custom-meta-box', // The id of our meta box.
'My Custom Meta Box', // The title of our meta box.
array( $this, 'wpturbo_render_meta_box_fields' ), // The callback function that renders the metabox.
$location, // The screen on which to show the box.
'normal', // The placement of our meta box.
'default', // The priority of our meta box.
);
}
}
// Render the meta box and its fields.
public function wpturbo_render_meta_box_fields($post){
wp_nonce_field( 'wpturbo-custom-meta-box_data', 'wpturbo-custom-meta-box_nonce' );
echo '<h3>My Custom Meta Box</h3>';
$html = '';
foreach( $this->fields as $field_id => $field ){
$meta_value = get_post_meta( $post->ID, $field_id, true );
if ( empty($meta_value) && isset( $field['default'] ) ) {
$meta_value = $field['default'];
}
$field_html = $this->wpturbo_render_input_field($field_id, $field, $meta_value);
$label = "<label for='$field_id'>{$field['label']}</label>";
$html .= $this->wpturbo_format_field($label, $field_html);
}
echo '<table class="form-table"><tbody>' . $html . '</tbody></table>';
}
// Format every field to table display.
public function wpturbo_format_field($label, $field){
return '<tr class="form-field"><th>'.$label.'</th><td>'.$field.'</td></tr>';
}
// Render each individual field.
public function wpturbo_render_input_field($field_id, $field, $field_value){
switch($field['type']){
case 'select': {
$field_html = '<select name="'.$field_id.'" id="'.$field_id.'">';
foreach($field['options'] as $key => $value){
$key = !is_numeric( $key ) ? $key : $value;
$selected = '';
if($field_value === $key){
$selected = 'selected="selected"';
}
$field_html .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
}
$field_html .= '</select>';
break;
}
case 'textarea': {
$field_html = '<textarea name="'.$field_id.'" id="'.$field_id.'" rows="6">'.$field_value.'</textarea>';
break;
}
default: {
$field_html = "<input type='{$field['type']}' id='$field_id' name='$field_id' value='$field_value' />";
break;
}
}
return $field_html;
}
// Called when this metabox is saved.
// Save the new meta values of our metabox.
public function wpturbo_save_meta_box_fields($post_id){
if ( ! isset( $_POST['wpturbo-custom-meta-box_nonce'] ) ) return $post_id;
$nonce = $_POST['wpturbo-custom-meta-box_nonce'];
if ( !wp_verify_nonce( $nonce, 'wpturbo-custom-meta-box_data' ) ) return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
foreach ( $this->fields as $field_id => $field ) {
if( isset($_POST[$field_id]) ){
// Sanitize fields that need to be sanitized.
switch($field['type']){
case 'email': {
$_POST[$field_id] = sanitize_email($_POST[$field_id]);
break;
}
case 'text': {
$_POST[$field_id] = sanitize_text_field($_POST[$field_id]);
break;
}
}
update_post_meta( $post_id, $field_id, $_POST[$field_id] );
}
}
}
}
if (class_exists('wpturbo-custom-meta-box')) {
new wpturbo-custom-meta-box();
}
Meta Box Generator
Related WordPress generators Term meta generator Post type generator Post status generator Go back to all generators