I just created a kind of long metabox using a tutorial at wptuts but I am lost in two things:
The output of the metabox displays all data grouped (ex. in grouped checkboxes they are all in a whole even if I have different groups) and I'd like to grab the titles of each group and display them in different tables, divs or blocks.
Don't know how to display the saved data in the post, and also display it in separate blocks.
Sorry for being such a noob heh but I want to learn how to code it for my themes, instead of using plugins. IM HUNGRY FOR KNOWLEDGE!
My code is this:
<?php
$prefix = 'dbt_';
$meta_box = array(
'id' => 'features',
'title' => 'Property Features',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Price (in USD)',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Price (in Mexican Peso)',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Address',
'desc' => '',
'id' => $prefix . 'textarea',
'type' => 'textarea',
'std' => ''
),
array(
'name' => 'Property for',
'id' => $prefix . 'select',
'type' => 'select',
'options' => array('Sale', 'Rent', 'Vacational Rental')
),
array(
'name' => 'Area (m2)',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Area of terrain (m2)',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Number of rooms',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Number of bathrooms. Include half bathrooms (ex. 2 1/2)',
'desc' => '',
'id' => $prefix . 'text',
'type' => 'text',
'std' => ' '
),
array(
'name' => 'Service Bathroom',
'id' => $prefix . 'checkbox',
'type' => 'checkbox'
),
array(
'name' => 'Furnished',
'id' => $prefix . 'checkbox',
'type' => 'checkbox'
),
array(
'label' => 'Property specs',
'desc' => '',
'id' => $prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Livingroom',
'value' => 'one'
),
'two' => array (
'label' => 'Diningroom',
'value' => 'two'
),
'three' => array (
'label' => 'Breackfast area',
'value' => 'three'
),
'four' => array (
'label' => 'TV room',
'value' => 'four'
),
'five' => array (
'label' => 'Studio',
'value' => 'five'
),
'six' => array (
'label' => 'Integrated Kitchen',
'value' => 'six'
),
'seven' => array (
'label' => 'Laundry Room',
'value' => 'seven'
),
'eight' => array (
'label' => 'Service room',
'value' => 'eight'
),
'nine' => array (
'label' => 'jacuzzi',
'value' => 'nine'
),
'ten' => array (
'label' => 'terrace',
'value' => 'ten'
),
'eleven' => array (
'label' => 'balcony',
'value' => 'eleven'
),
'twelve' => array (
'label' => 'Warehouse',
'value' => 'twelve'
)
)
),
array(
'label' => 'Installed',
'desc' => '',
'id' => $prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Air conditioner',
'value' => 'one'
),
'two' => array (
'label' => 'Stationary gas',
'value' => 'two'
),
'three' => array (
'label' => 'Water heater',
'value' => 'three'
),
'four' => array (
'label' => 'Hurricain shutters or hurricain protection',
'value' => 'four'
),
'five' => array (
'label' => 'Telephone line',
'value' => 'five'
)
)
),
array(
'label' => 'Outdoors',
'desc' => '',
'id' => $prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Parking space',
'value' => 'one'
),
'two' => array (
'label' => 'Garden',
'value' => 'two'
),
'three' => array (
'label' => 'Pool',
'value' => 'three'
),
'four' => array (
'label' => 'Tennis court',
'value' => 'four'
),
'five' => array (
'label' => 'Children playground',
'value' => 'five'
)
)
),
array(
'label' => 'Facilities',
'desc' => '',
'id' => $prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Spa',
'value' => 'one'
),
'two' => array (
'label' => 'Gym',
'value' => 'two'
),
'three' => array (
'label' => 'Party salon',
'value' => 'three'
),
'four' => array (
'label' => 'Snack-bar',
'value' => 'four'
)
)
),
array(
'label' => 'Luxury Features',
'desc' => '',
'id' => $prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Clubhouse',
'value' => 'one'
),
'two' => array (
'label' => 'Dock',
'value' => 'two'
),
'three' => array (
'label' => 'Waterfront',
'value' => 'three'
),
'four' => array (
'label' => 'Golf course',
'value' => 'four'
)
)
)
)
);
add_action('admin_menu', 'mytheme_add_box');
// Add meta box
function mytheme_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
// checkbox_group
case 'checkbox_group':
foreach ($field['options'] as $option) {
echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
<label for="'.$option['value'].'">'.$option['label'].'</label><br />';
}
echo '<span class="description">'.$field['desc'].'</span>';
break;
}
echo '</td><td>',
'</td></tr>';
}
echo '</table>';
}
do_action('save_post', 'mytheme_save_data');
// Save data from meta box
function mytheme_save_data($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_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;
}
foreach ($meta_box['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);
}
}
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
HTML
and/orCSS
question about formatting.