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 have a metabox with two static fields and one more field I add in ingredients_show_box function. All three fields are visible by $_POST when I save data. But the two static fields are visible by metabox and I can update data via foreach, and third is not visible. It's not suitable as I would like to add a variable number of fields with unique field names for each post.

$food_box = array(
'id' => 'foodbox',
'title' => __('Ingredients details','splus'),
'page' => 'recipes',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
    array(
        'name' => __('Ingredients','splus'),
        'desc' => __('List all the ingredients','splus'),
        'id' => 'tingred',
        'type' => 'text',
        'std' => ''
    ),
    array(
        'name' => __('Tips','splus'),
        'desc' => __('Tips for this recipe','splus'),
        'id' => 'ttips',
        'type' => 'text',
        'std' => '1'
    )
));

add_action('admin_menu', 'mytheme_add_box');  // Add meta box
function mytheme_add_box() {
global $meta_box, $food_box;
add_meta_box($food_box['id'], $food_box['title'], 'ingredients_show_box', $food_box['page'], $food_box['context'], $food_box['priority']);}

function ingredients_show_box() {
global $food_box, $post, $arr;
echo '<input type="hidden" name="food_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; // Use nonce for verification

echo '<table class="form-table">';
$arr = array ( 'name' => 'n11', 
    'desc' => 'n11', 
    'id' => 'n11',
    'type' => 'text',
   'std' => '464');

$food_box['fields'][] = $arr; // insert additional field

foreach ($food_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);
    // populating the fields here
}
echo '</table>';}

add_action('save_post', 'mytheme_save_data');   // Save recipe post from meta box

 

function mytheme_save_data($post_id) {
global $meta_box, $food_box;
global $wpdb;
//die( var_dump( $_POST, $_GET, $_REQUEST ) );

// UNFORTUNATELLY $foodbox does not include filed 'n11'
foreach ($food_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);
    }
}
$new = $_POST['n11']; // workaround and not suitable: $_POST has this field
update_post_meta($post_id, 'n11', $new);
}
share|improve this question
    
That is a lot of code, and it has formatting issues, but I don't see where you set $food_box['n11']. If I have missed that please point out the line where that occurs. It should come through in the $_POST array, though. I don't know why you aren't using that, or think it is a "workaround". –  s_ha_dum Apr 18 '13 at 14:23
    
Your problem is that you're relying on an array to save data, when, in fact, you cannot push anything into it because it gets re-initialized with the same two elements on every request. You can check this question, or this one for guidance on how to rely on what's in the $_POST instead. –  dalbaeb Apr 18 '13 at 14:49
    
I add the $food_box['n11'] by the next code:$arr = array ( 'name' => 'n11', 'desc' => 'n11', 'id' => 'n11', 'type' => 'text', 'std' => '464'); $food_box['fields'][] = $arr; // insert additional field –  Petro Apr 18 '13 at 18:19
    
dalbaeb, thanks for examples - i'll try to do something like quastion-2 –  Petro Apr 18 '13 at 18:42
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.