0

I've been working for days to get this working without luck. Hope someone can help me.

I created multiple metaboxes but my backend shows the boxes empty, I am lost and don't know where to go with this. Note that I am not closed to use multiple metaboxes as long as I can divide in blocks the info but I'm a noob...

so any help is accepted. Please be patient because I need a detailed explanation.

My code is:

$prefix = 'ikos_';
$metaboxWrapper = array();

$metaboxWrapper[] =
$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'
    )
));

Then I have more metaboxes with checkbox groups like this:

$metaboxWrapper[] =
$meta_box = array(  
'id' => 'installed',
'title' => 'Installed',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'label' => 'Installed',  
'desc'  => '',  
'type'  => 'checkbox_group', 
        'fields' => array(  
'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'  
    )
)  
));

And to show them I have this but doesn't work. I can't even go to the part of saving and showing data in front end...so I don't know if it works. Still I pase it here so you can point me if I am doing it wrong.

add_action('admin_menu', 'mytheme_add_box');

// Add meta box
function mytheme_add_box() {
global $metaboxWrapper;

foreach($metaboxWrapper as $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 $metaboxWrapper;
// 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;
}
9
  • Do you have debugging information? You mention "front end" several times. Are you trying to show the meta boxes on the front end of the site?
    – s_ha_dum
    Jun 24, 2013 at 15:12
  • oh snap! you are right, I meant backend the first time I wrote it. The post is edited tho.. About debugging...I get this: Undefined index: id in /home/homesmore/homes-more.com.mx/wp-content/themes/ElegantEstate/metabox.php on line 329 Notice: Undefined index: name in /home/homesmore/homes-more.com.mx/wp-content/themes/ElegantEstate/metabox.php on line 331 Notice: Undefined index: type in /home/homesmore/homes-more.com.mx/wp-content/themes/ElegantEstate/metabox.php on line 333 Jun 24, 2013 at 15:29
  • See wordpress.stackexchange.com/a/91406/21376 for the undefined index issues. You should fix those but I don't think that will fix your problem.
    – s_ha_dum
    Jun 24, 2013 at 15:35
  • I see that the lines that have errors are a lot of lines, the ones that have $field in it.. Jun 24, 2013 at 15:41
  • 2
    One way to narrow down a problem is to remove the bulk of it, I suggest you strip out everything except one simple array and try to output it where you want, since it looks like you have some PHP issues. It aslo makes it easier for people to look it over to answer.
    – Wyck
    Jun 24, 2013 at 15:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.