Multiple Custom Meta Boxes & Fields
-
I have a Custom Post Type that has multiple Custom Meta Boxes, each with multiple Custom Fields. The code is a bit overwhelming and I wondering if there might be a better way to go about doing this.
// Add the DATE OF BIRTH and DEATH DATE Meta Box function add_bldob_meta_box() { add_meta_box( 'bldob_meta_box', // $id 'Date of Birth and Death', // $title 'show_bldob_meta_box', // $callback 'obituaries', // $page 'normal', // $context 'default'); // $priority } add_action('add_meta_boxes', 'add_bldob_meta_box'); // Field Array $prefix = 'bldob_'; $bldob_meta_fields = array( array( 'label' => 'Date of Birth', 'desc' => '', 'id' => $prefix.'bdate', 'type' => 'date' ), array( 'label' => 'Date of Death', 'desc' => '', 'id' => $prefix.'ddate', 'type' => 'date' ) ); function show_bldob_meta_box() { global $bldob_meta_fields, $post; // Use nonce for verification echo '<input type="hidden" name="bldob_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; // Begin the field table and loop echo '<table class="form-table">'; foreach ($bldob_meta_fields as $field) { // get value of this field if it exists for this post $meta = get_post_meta($post->ID, $field['id'], true); // begin a table row with echo '<tr> <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> <td>'; switch($field['type']) { // case items will go here // Date of Birth case 'date': echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // Date od Death case 'date': echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; } //end switch echo '</td></tr>'; } // end foreach echo '</table>'; // end table } // Save the Data function save_bldob_meta($post_id) { global $bldob_meta_fields; // verify nonce if (!wp_verify_nonce($_POST['bldob_meta_box_nonce'], basename(__FILE__))) return $post_id; // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields and save the data foreach ($bldob_meta_fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } // end foreach } add_action('save_post', 'save_bldob_meta'); // Add the VISITATION Meta Box function add_blvis_meta_box() { add_meta_box( 'blvis_meta_box', // $id 'Visitation Information', // $title 'show_blvis_meta_box', // $callback 'obituaries', // $page 'normal', // $context 'default'); // $priority } add_action('add_meta_boxes', 'add_blvis_meta_box'); // Field Array $prefix = 'blvis_'; $blvis_meta_fields = array( array( 'label' => 'Date', 'desc' => '', 'id' => $prefix.'date', 'type' => 'date' ), array( 'label'=> 'Time', 'desc' => '', 'id' => $prefix.'time', 'type' => 'text' ), array( 'label'=> 'Location Name', 'desc' => '', 'id' => $prefix.'text', 'type' => 'text' ), array( 'label'=> 'Location Address', 'desc' => '', 'id' => $prefix.'address', 'type' => 'textarea' ), array( 'label'=> 'Details', 'desc' => '', 'id' => $prefix.'textarea', 'type' => 'textarea' ) ); function show_blvis_meta_box() { global $blvis_meta_fields, $post; // Use nonce for verification echo '<input type="hidden" name="blvis_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; // Begin the field table and loop echo '<table class="form-table">'; foreach ($blvis_meta_fields as $field) { // get value of this field if it exists for this post $meta = get_post_meta($post->ID, $field['id'], true); // begin a table row with echo '<tr> <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> <td>'; switch($field['type']) { // case items will go here // Date case 'date': echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // Time case "time": echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // location name case 'text': echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // address case 'address': echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> <br /><span class="description">'.$field['desc'].'</span>'; break; // details case 'textarea': echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> <br /><span class="description">'.$field['desc'].'</span>'; break; } //end switch echo '</td></tr>'; } // end foreach echo '</table>'; // end table } // Save the Data function save_blvis_meta($post_id) { global $blvis_meta_fields; // verify nonce if (!wp_verify_nonce($_POST['blvis_meta_box_nonce'], basename(__FILE__))) return $post_id; // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields and save the data foreach ($blvis_meta_fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } // end foreach } add_action('save_post', 'save_blvis_meta'); // Add the SERVICE Meta Box function add_blser_meta_box() { add_meta_box( 'blser_meta_box', // $id 'Service Information', // $title 'show_blser_meta_box', // $callback 'obituaries', // $page 'normal', // $context 'default'); // $priority } add_action('add_meta_boxes', 'add_blser_meta_box'); // Field Array $prefix = 'blser_'; $blser_meta_fields = array( array( 'label' => 'Date', 'desc' => '', 'id' => $prefix.'date', 'key' => 'servicedate', 'type' => 'date' ), array( 'label'=> 'Time', 'desc' => '', 'id' => $prefix.'time', 'type' => 'text' ), array( 'label'=> 'Location Name', 'desc' => '', 'id' => $prefix.'text', 'type' => 'text' ), array( 'label'=> 'Location Address', 'desc' => '', 'id' => $prefix.'address', 'type' => 'textarea' ), array( 'label'=> 'Details', 'desc' => '', 'id' => $prefix.'textarea', 'type' => 'textarea' ) ); function show_blser_meta_box() { global $blser_meta_fields, $post; // Use nonce for verification echo '<input type="hidden" name="blser_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; // Begin the field table and loop echo '<table class="form-table">'; foreach ($blser_meta_fields as $field) { // get value of this field if it exists for this post $meta = get_post_meta($post->ID, $field['id'], true); // begin a table row with echo '<tr> <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> <td>'; switch($field['type']) { // case items will go here // Date case 'date': echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // Time case "time": echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // location name case 'text': echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> <br /><span class="description">'.$field['desc'].'</span>'; break; // address case 'address': echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> <br /><span class="description">'.$field['desc'].'</span>'; break; // details case 'textarea': echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> <br /><span class="description">'.$field['desc'].'</span>'; break; } //end switch echo '</td></tr>'; } // end foreach echo '</table>'; // end table } // Save the Data function save_blser_meta($post_id) { global $blser_meta_fields; // verify nonce if (!wp_verify_nonce($_POST['blser_meta_box_nonce'], basename(__FILE__))) return $post_id; // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields and save the data foreach ($blser_meta_fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } // end foreach } add_action('save_post', 'save_blser_meta');
I want to make sure I am doing things the best efficient way. The site currently works great for the client but I really want to be able to showcase it as an awesome custom WordPress solution (like in the WP community & with other developers).
Any suggestions are greatly appreciated!
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Multiple Custom Meta Boxes & Fields’ is closed to new replies.