Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am writing a plugin inside a class and i have created my $meta_boxes, i am able to retrieve all fields but they are duplicated and in the same table.

this is my code for add $meta_boxes

function ri_add_custom_box() {
    global $prefix, $meta_box, $meta_boxes;


$prefix = '_ri_custom_meta_'; // a custom prefix to help us avoid pulling data from the wrong meta box
$meta_boxes = array();

$meta_boxes[] = array(  
'id' => 'ri_meta_box_panel1', // the id of our meta box
'title' => 'Test1', // the title of the meta box
'page' => 'ri_my_page', // display this meta box on post editing screens
'context' => 'side',
'priority' => 'high', // keep it near the top
    'fields' => array( // all of the options inside of our meta box
        array(
            'name' => 'Test1:',
            'desc' => 'Test1',
            'id' => $prefix . 'Test1',
            'type' => 'text',
            'std' => ''
        ),
    )
);

$meta_boxes[] = array(  
'id' => 'ri_meta_box_panel2', // the id of our meta box
'title' => 'Test2', // the title of the meta box
'page' => 'ri_my_page', // display this meta box on post editing screens
'context' => 'side',
'priority' => 'high', // keep it near the top
    'fields' => array( // all of the options inside of our meta box
        array(
            'name' => 'Test2:',
            'desc' => 'Test2',
            'id' => $prefix . 'Test2',
            'type' => 'text',
            'std' => ''
        ),
    )
);

foreach ($meta_boxes as $meta_box) {
    new Ri($meta_box);
    add_meta_box($meta_box['id'], $meta_box['title'], array(&$this, 'ri_display_metaboxes'), $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
}

and this is my code for display $meta_boxes

function ri_display_metaboxes() {
    global $meta_box, $meta_boxes, $post;

    echo '<input type="hidden" name="ri_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    echo '<table class="form-table">';

        foreach ($meta_boxes as $meta_box) {
            foreach ($meta_box['fields'] as $field) {

            $meta = get_post_meta($post->ID, $field['id'], true);

    echo '<tr>', // create a table row for each option
                '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {

            case 'text': // the HTML to display for type=text options
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '
', $field['desc'];
                break;     

            case 'textarea': // the HTML to display for type=textarea options
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '
', $field['desc'];
                break;

            case 'select': // the HTML to display for type=select options
                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': // the HTML to display for type=radio options
                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': // the HTML to display for type=checkbox options
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                break;

        }
        echo     '<td>',
            '</tr>';
                }
            }
        echo '</table>';
}

I want to display in the first metabox Test1 and in second metabox Test2 like this Metabox1 Test1

Metabox2 Test2

but now with this code i have

Metabox1 Test1 Test2

Metabox2 Test1 Test2

Sorry for my bad english. Any help is appreciated!

share|improve this question
    
You're adding the metaboxes two times. You need to verify if they already exists or better make a 1:1 relation between Metabox:Test, so Metabox1:Test1 and Metabox2:Test2. I assume that's because that you execute the code two times, once per each Metabox instance. You need to decouple the two. –  hakre Jul 12 '11 at 15:06
    
I add metaboxes only one time! I dont know how decouple the two. Thanks in advance! –  mic3000 Jul 12 '11 at 15:25
    
Maybe two instance of the class that adds it one time and therefore two times? –  hakre Jul 12 '11 at 15:29
add comment

3 Answers

The Problem is in your ri_display_metaboxes() function more specifically this line:

foreach ($meta_boxes as $meta_box) {

Which tells WordPress to render both/all metaboxes on every single metabox display callback.

to get over it you can use the $callback_args parameter of add_meta_box function

so try this:

function ri_add_custom_box() {
    global $prefix, $meta_box, $meta_boxes;


    $prefix = '_ri_custom_meta_'; // a custom prefix to help us avoid pulling data from the wrong meta box
    $meta_boxes = array();

    $meta_boxes[] = array(  
    'id' => 'ri_meta_box_panel1', // the id of our meta box
    'title' => 'Test1', // the title of the meta box
    'page' => 'ri_my_page', // display this meta box on post editing screens
    'context' => 'side',
    'priority' => 'high', // keep it near the top
        'fields' => array( // all of the options inside of our meta box
            array(
                'name' => 'Test1:',
                'desc' => 'Test1',
                'id' => $prefix . 'Test1',
                'type' => 'text',
                'std' => ''
            ),
        )
    );

    $meta_boxes[] = array(  
    'id' => 'ri_meta_box_panel2', // the id of our meta box
    'title' => 'Test2', // the title of the meta box
    'page' => 'ri_my_page', // display this meta box on post editing screens
    'context' => 'side',
    'priority' => 'high', // keep it near the top
        'fields' => array( // all of the options inside of our meta box
            array(
                'name' => 'Test2:',
                'desc' => 'Test2',
                'id' => $prefix . 'Test2',
                'type' => 'text',
                'std' => ''
            ),
        )
    );

    foreach ($meta_boxes as $meta_box) {
        new Ri($meta_box);
        add_meta_box($meta_box['id'], $meta_box['title'], array(&$this, 'ri_display_metaboxes'), $meta_box['page'], $meta_box['context'], $meta_box['priority'],array('fields' => $meta_box['fields']));
    }
}


function ri_display_metaboxes($post,$meta_b) {
    global $meta_boxes, $post;

    echo '<input type="hidden" name="ri_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    echo '<table class="form-table">';

        foreach ((array)$meta_b['arg']['fields'] as $field) {
            $meta = get_post_meta($post->ID, $field['id'], true);
            echo '<tr>', // create a table row for each option
                '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
            switch ($field['type']) {
                case 'text': // the HTML to display for type=text options
                    echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '', $field['desc'];
                    break;     
                case 'textarea': // the HTML to display for type=textarea options
                    echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '', $field['desc'];
                    break;
                case 'select': // the HTML to display for type=select options
                    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': // the HTML to display for type=radio options
                    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': // the HTML to display for type=checkbox options
                    echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                    break;
            }
            echo     '<td>',
                '</tr>';
        }
    echo '</table>';
}
share|improve this answer
    
Thanks for your replies. Bainternet i am trying with your suggestions, but i have this error Warning: Invalid argument supplied for foreach() on this line foreach ($meta_box['fields'] as $field) { Thanks in advance. –  mic3000 Jul 13 '11 at 10:42
    
try changing $m[id] to $m['id'] –  Bainternet Jul 13 '11 at 10:54
    
I have already tried! –  mic3000 Jul 13 '11 at 11:20
    
ok i've updated the code, try it now –  Bainternet Jul 13 '11 at 11:55
    
I have tried with latest changes but my metaboxes are empty now –  mic3000 Jul 13 '11 at 13:26
add comment

I have found the error!

foreach ((array)$meta_b['args']['fields'] as $field) {

instead of

foreach ((array)$meta_b['arg']['fields'] as $field) {
share|improve this answer
add comment

How can i save post data now?

this is my code:

function ri_meta_save_data($post_id) {
    global $post;

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // verify nonce -- checks that the user has access
    if (!wp_verify_nonce($_POST['ri_meta_box_nonce'], basename(__FILE__))) {
        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 ((array)$meta_b['args']['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);
        }
    }
}

Thanks in advance!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.