0
votes

When I save post, data in metabox aren't saved in database...where is the bug ?

My code is here at: pastebin.

Thanks!

1

1 Answers

0
votes

Actually you have declared your $rest_custom_meta_fields array inside restaurant_data_form function and trying to use it in save_restaurant_custom_meta function, in this case the array is out of the function scope so foreach ($rest_custom_meta_fields as $field) is not working.

To overcome this problem you can keep the array out of your restaurant_data_form, just declare the array right before the restaurant_data_form function like

$rest_custom_meta_fields = array(  
    array(  
        'label'=> 'Address',  
        'desc'  => 'Plugin use it to get map',  
        'id'    => $prefix.'text_address',  
        'type'  => 'text'  
    ),
    ...
);

And in your restaurant_data_form function

function restaurant_data_form()
{
    $prefix = 'rest_';
    global $post, $rest_custom_meta_fields;
    // ...
}

So it should look something like this (the array is in the global scope)

$rest_custom_meta_fields = array( 
    array(...),
    ...
);
function restaurant_data_form()
{
    $prefix = 'rest_';
    global $post, $rest_custom_meta_fields;
    // ...
}

I hope this will solve the problem. Also at the end of your code you have used

echo add_action('save_post', 'save_restaurant_custom_meta');

Remove the echo from the beginning of the add_action(...) statement.