I am working with a recipes blog and I use custom meta boxes.
$food_box = array(
'id' => 'foodbox',
'title' => __('Ingredients details','splus'),
'page' => 'recipes',
'context' => 'normal',
'priority' => 'high',
'fields' => array(),);
$arr = array ( 'name' => '-', 'desc' => '-', 'id' => '-','type' => 'text', 'std' => '');
add_action('admin_menu', 'mytheme_add_box'); // Add meta box
function mytheme_add_box() {
global $meta_box;
global $food_box;
global $test_box;
add_meta_box(
$meta_box['id'],
$meta_box['title'],
'recipe_details_show_box',
$meta_box['page'],
$meta_box['context'],
$meta_box['priority']
);
add_meta_box(
$food_box['id'],
$food_box['title'],
'ingredients_show_box',
$food_box['page'],
$food_box['context'],
$food_box['priority']
);
}
Acoording to recipe ingredients are devided to sets of food and the same food item can be present 2 time in one recipe (for example, 200gr sugar for dough and 50 gr for stuff). To combine two taxonomies (food items and food sets) I created custom table and I add fileds $food_box['fields'][] = $arr;
from this table by next code:
foreach ( $terms as $term ) { // show tabs content for foodset
$s = ($i==1) ? ' ': ' style="display: none;"';
echo '<div id="set-'.$term->term_id .'" class="tabs-panel"' . $s . '>';
echo '<ul id="set2-checklist" class="categorychecklist form-no-clear" >';
echo '<table id="food_data_table_header" class="form-table" border="0" align="left">';
echo '<tr>'; //show header
echo '<td style="width: 50%" > <span>FoodSet</span> </td>';
echo '<td style="width: 50%" > <span>FoodId</span> </td>';
echo '<td style="width:100%" > <span>Description</span> </td>';
echo '<td style="width: 50%" > <span>FoodUnit</span> </td>';
echo '<td style="width: 50%" > <span>Quantity</span> </td>';
echo '<td style="width: 50%" > <span>Order</span> </td>';
echo '<td style="width:100%" > <span>Comment</span> </td>';
echo '</tr>';
foreach ( $foods as $food ) { //show data
$food_row = $wpdb->get_row($wpdb->prepare(
"SELECT rf.post_id, rf.foodset_id, rf.food_id, tt1.description, rf.food_unit, rf.quantity, rf.food_order, rf.food_comment
FROM $wpdb->term_taxonomy tt1 JOIN $wpdb->recipefood rf ON rf.food_id = tt1.term_id
WHERE (rf.post_id=".$post_id.") AND (rf.foodset_id=".$term->term_id.") AND (rf.food_id=".$food->term_id.")"));
$food_set = $term->term_id;
$food_id = $food->term_id;
$id = $food_set . '-' . $food_id;
$food_name = $food_row->description;
echo '<tr>';
echo '<td>',$food_set,'</td>';
echo '<td>',$food_id,'</td>';
echo '<td>',$food_name,'</td>';
$fls = array ('food_unit','quantity','food_order','food_comment');
foreach ($fls as $fl) {
switch ($fl) {
case 'food_unit':
$def_val = $def_unit; break;
case 'quantity':
$def_val = $def_qty; break;
case 'food_order':
$def_val = '1'; break;
case 'food_comment':
$def_val = 'comment'; break;
}
$meta = ($food_row->$fl == '') ? $def_val : $food_row->$fl;
$arr = array ( 'name' => $fl.'-'. $id, 'desc' => $fl.'-'. $id, 'id' => $fl.'-'. $id,'type' => 'text', 'std' => $meta);
$food_box['fields'][] = $arr; //add new field to array
echo '<td>','<input type="text" name="'.$fl.'-'.$id. '" id="'.$fl.'-'.$id.'" value="',$meta , '" style="width: 50%"/>', '</td>';
// $meta ? $meta : $food_box[$fl.'-'.$id]['std']
}
echo '</tr>';
But unfortunately I can not save my updated data as $_POST['quantity-708-481']
is empty and not defined in
add_action('save_post', 'mytheme_save_data'); // Save recipe post from meta box
function mytheme_save_data($post_id) {
global $meta_box, $food_box;
//...
$wpdb->update( $wpdb->recipefood,
array( //set
'food_comment' => $_POST['quantity-708-481']
), array( //where
'post_id' => '1',
'foodset_id' => '822',
'food_id' => '484'
)
);
In other words, the field is added in meta box but not visible during saving. Any suggestion are more than welcomed.
I am working with a recipes blog and I use custom meta boxes.
$food_box = array(
'id' => 'foodbox',
'title' => __('Ingredients details','splus'),
'page' => 'recipes',
'context' => 'normal',
'priority' => 'high',
'fields' => array(),);
$arr = array ( 'name' => '-', 'desc' => '-', 'id' => '-','type' => 'text', 'std' => '');
add_action('admin_menu', 'mytheme_add_box'); // Add meta box
function mytheme_add_box() {
global $meta_box;
global $food_box;
global $test_box;
add_meta_box(
$meta_box['id'],
$meta_box['title'],
'recipe_details_show_box',
$meta_box['page'],
$meta_box['context'],
$meta_box['priority']
);
add_meta_box(
$food_box['id'],
$food_box['title'],
'ingredients_show_box',
$food_box['page'],
$food_box['context'],
$food_box['priority']
);
}
Acoording to recipe ingredients are devided to sets of food and the same food item can be present 2 time in one recipe (for example, 200gr sugar for dough and 50 gr for stuff). To combine two taxonomies (food items and food sets) I created custom table and I add fileds $food_box['fields'][] = $arr;
from this table by next code:
foreach ( $terms as $term ) { // show tabs content for foodset
$s = ($i==1) ? ' ': ' style="display: none;"';
echo '<div id="set-'.$term->term_id .'" class="tabs-panel"' . $s . '>';
echo '<ul id="set2-checklist" class="categorychecklist form-no-clear" >';
echo '<table id="food_data_table_header" class="form-table" border="0" align="left">';
echo '<tr>'; //show header
echo '<td style="width: 50%" > <span>FoodSet</span> </td>';
echo '<td style="width: 50%" > <span>FoodId</span> </td>';
echo '<td style="width:100%" > <span>Description</span> </td>';
echo '<td style="width: 50%" > <span>FoodUnit</span> </td>';
echo '<td style="width: 50%" > <span>Quantity</span> </td>';
echo '<td style="width: 50%" > <span>Order</span> </td>';
echo '<td style="width:100%" > <span>Comment</span> </td>';
echo '</tr>';
foreach ( $foods as $food ) { //show data
$food_row = $wpdb->get_row($wpdb->prepare(
"SELECT rf.post_id, rf.foodset_id, rf.food_id, tt1.description, rf.food_unit, rf.quantity, rf.food_order, rf.food_comment
FROM $wpdb->term_taxonomy tt1 JOIN $wpdb->recipefood rf ON rf.food_id = tt1.term_id
WHERE (rf.post_id=".$post_id.") AND (rf.foodset_id=".$term->term_id.") AND (rf.food_id=".$food->term_id.")"));
$food_set = $term->term_id;
$food_id = $food->term_id;
$id = $food_set . '-' . $food_id;
$food_name = $food_row->description;
echo '<tr>';
echo '<td>',$food_set,'</td>';
echo '<td>',$food_id,'</td>';
echo '<td>',$food_name,'</td>';
$fls = array ('food_unit','quantity','food_order','food_comment');
foreach ($fls as $fl) {
switch ($fl) {
case 'food_unit':
$def_val = $def_unit; break;
case 'quantity':
$def_val = $def_qty; break;
case 'food_order':
$def_val = '1'; break;
case 'food_comment':
$def_val = 'comment'; break;
}
$meta = ($food_row->$fl == '') ? $def_val : $food_row->$fl;
$arr = array ( 'name' => $fl.'-'. $id, 'desc' => $fl.'-'. $id, 'id' => $fl.'-'. $id,'type' => 'text', 'std' => $meta);
$food_box['fields'][] = $arr; //add new field to array
echo '<td>','<input type="text" name="'.$fl.'-'.$id. '" id="'.$fl.'-'.$id.'" value="',$meta , '" style="width: 50%"/>', '</td>';
// $meta ? $meta : $food_box[$fl.'-'.$id]['std']
}
echo '</tr>';
But unfortunately I can not save my updated data as $_POST['quantity-708-481']
is empty and not defined in
add_action('save_post', 'mytheme_save_data'); // Save recipe post from meta box
function mytheme_save_data($post_id) {
global $meta_box, $food_box;
//...
$wpdb->update( $wpdb->recipefood,
array( //set
'food_comment' => $_POST['quantity-708-481']
), array( //where
'post_id' => '1',
'foodset_id' => '822',
'food_id' => '484'
)
);
In other words, the field is added in meta box but not visible during saving. Any suggestion are more than welcomed.
Share Improve this question edited Apr 13, 2013 at 22:32 Petro asked Apr 12, 2013 at 21:41 PetroPetro 11 bronze badge 3 |1 Answer
Reset to default 1$fl
is 'quantity', but from your code snippet it is not clear what $id
is set to. I think it is something like 481
or 708
. So name="'.$fl.'-'.$id. '"
will result in quantity-481
or quantity-708
and not quantity-708-481
.
Check the HTML of your metabox if the name is what you expect (use something like FireBug in FireFox or the Developer Tools in Chrome).
$fl.'-'. $id
are quite confusing... Compare the HTML with the output ofvar_dump($_POST);die();
inside the functionmytheme_save_data
. I'm voting to close as too-localized as this looks too much to a wrong var names problem and debugging all this is a task for you. – brasofilo Commented Apr 12, 2013 at 22:18id
when you output, you shouldn't hard-code it when you're saving as well. – montrealist Commented Apr 12, 2013 at 22:18