I’m currently working on creating meta boxes. I used the following tutorial and some self-adapted. Link of tutorial: http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/
Now I get the following error message:
Notice: Undefined index: dsmeta_image in
/customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php
on line 181 Notice: Undefined index: dsmeta_image_caption in
/customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php
on line 181
It seems that the variable does not exist, I’m using an array fields for Metabox and created a foreach loop walk you through it if I understand correctly.
How is this problem.
It is in any event error when saving the meta boxes …
The part of setting up the fields array:
// Create the fields array
$prefix = 'dsmeta_';
$custom_meta_fields = array(
array(
'label' => 'Image',
'desc' => '',
'id' => $prefix . 'image',
'type' => 'image'
),
array(
'label' => 'Image caption',
'desc' => '',
'id' => $prefix . 'image_caption',
'type' => 'text'
)
);
Part of the saving function:
add_action('save_post', 'dsslider_manager_save_extras');
function dsslider_manager_save_extras($post_id) {
global $custom_meta_fields;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_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);
}
} // end foreach
}
Update after request
Here I add the meta box for the fields :
// Add meta box support
// This registers a function to be called when the WordPress admin interface is visited
add_action("admin_init", "dsslider_manager_add_meta");
function dsslider_manager_add_meta(){
// Create this cool new meta box for Portfolio Options
add_meta_box("dsslider-meta", "Brandbox Options", "dsslider_manager_meta_options", "brandbox-slider", "normal", "high");
}
And here is the function for building the meta fields :
function dsslider_manager_meta_options(){
global $custom_meta_fields, $post;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
// (integer) (optional) The post ID whose custom fields will be retrieved.
// Default: Current post
return $post_id;
?>
<div class="dsslider_manager_extras">
<div class="ds-metabox" data-max_rows="5" data-min_rows="0">
<table class="meta ds-input-table">
<?php
foreach ($custom_meta_fields as $field) {
$custom = get_post_meta($post->ID, $field['id'], true); // Returns a multidimensional array with all custom fields of a particular post or page.
// Past HTML markup
?>
<tbody class="ui-sortable">
<?php
echo '<tr class="row">';
echo '<td class="order"></td>';
echo '<td>';
switch($field['type']) {
// case items will go here
// image
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if($custom) {
$image = wp_get_attachment_image_src($custom, 'thumbnail');
$image = $image[0];
} // end if statement
echo '<img src="' . $image . '" class="custom_preview_image" alt="" />
<input type="button" class="button add-image" name="' . $field['id'] . '" value="' . $custom . '"><a href="#" class="remove-image">Remove Image</a>';
break;
// text
case 'text':
echo '<input type="text" class="text" name="' . $field['id'] . '" value="' . $custom . '">';
break;
} //end switch
echo '</td>';
echo '</tr>';
} // End foreach loop
?>
</tbody>
</table><!-- End .meta ds-input-table -->
<ul class="ds-repeater-footer hl clearfix">
<li class="right">
<a href="#" class="repeatable-add ds-button">Add New Slide</a>
</li>
</ul><!-- End ul.hl clearfix repeater-footer -->
</div><!-- End .ds-metabox -->
</div><!-- End .dsslider_manager_extras -->
<?php
}
The problem is that you’re using the $custom_meta_fields array to both generate your input fields and grab information from the $_POST array using complimentary key names.
This wouldn’t ordinarily be a problem, but the fact is that some of the fields that you’re using aren’t actually passing information to the $_POST array. An example:
In that foreach loop, you’re attempting to grab a variable $_POST[‘dsmeta_image’] which doesn’t exist, as your form never passes that particular key. A simple fix would be something like this:
You also need to bear in mind that input fields of type Button DO NOT send information to the $_POST array. If this was your intention, you need to send the information you want via Hidden Field, or something else.
Hope this helps.