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 . '">Remove Image';
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">
Add New Slide
</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:
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 . '">Remove Image';
break;
//Later on....
foreach ($custom_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']]; //<-- BOOM
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
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:
foreach ($custom_meta_fields as $field) {
if(isset($_POST[$field['id'])){
$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);
}
}
else
continue;
} // end foreach
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.
Related
I've a problem to show a Custom Menu in sidebar.
Now, I've created a Menu from WordPress back-end. this menu must be showed in sidebar of my template, with menu name (i.e. CUSTOM MENU) and the structure with parents and child.
My target is this.
At the moment, I've this part of code:
$meta_box_menu = array(
'id' => 'custom-meta-menu',
'title' => 'Menu Sidebar',
'page' => 'page',
'context' => 'side',
'priority' => 'high',
'fields' => array(
array(
'id' => 'custom-meta-menu-name',
'type' => 'select',
'std' => 'none'
),
),
);
/*
* This function will register the meta box with WordPress
*/
function custom_add_box() {
global $meta_box_menu;
add_meta_box($meta_box_menu['id'], $meta_box_menu['title'], 'custom_meta_menu_html',
$meta_box_menu['page'], $meta_box_menu['context'], $meta_box_menu['priority']);
}
add_action('admin_init', 'custom_add_box');
/*
* This function will produce the html needed to display our meta box in the admin area
*/
function custom_meta_menu_html() {
global $meta_box_menu, $post;
$output = '<p style="padding:10px 0 0 0;">'.__('Scegli il menu da mostrare nella Sidebar di questa pagina.', 'custom').'</p>';
$output .= '<input type="hidden" name="sf_meta_box_nonce" value="'.
wp_create_nonce(basename(__FILE__)). '" />';
$output .= '<table class="form-table">';
foreach ($meta_box_menu['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
/*
* Get out all our menus using the function from functions.php
*/
$menus = custom_get_all_menus();
/*
* Grab out saved data for edit mode
*/
$meta = get_post_meta($post->ID, $field['id'], true);
$output .= '<select name="'.$field['id'].'" class="widefat">';
$output .= '<option value="none">- none -</option>';
if(is_array($menus)):
foreach($menus as $k => $v):
if($meta==$v->slug):
$output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
else:
$output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
endif;
endforeach;
endif;
$output .= '</select>';
}
$output .= '</table>';
echo $output;
}
/*
* This function will save our preferences into the database
*/
function custom_save_data($post_id) {
global $meta_box, $meta_box_menu;
foreach ($meta_box_menu['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'], stripslashes(htmlspecialchars($new)));
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'custom_save_data');
function custom_get_all_menus() {
$menu_obj = get_terms( 'nav_menu' );
return $menu_obj;
}
/*
* Html Custom Sidebar Menu
*/
function custom_generate_menu_from_items($items) {
if(count($items)>0):
$menu_list = '<aside class="widget sidebar_menu"><h5 class="widget-title">' .'CUSTOM MENU'. '</h5><nav><ul class="sidebar-menu-items">';
foreach ( (array) $items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li>' . $title . '</li>';
}
$menu_list .= '</ul></nav></aside>';
return $menu_list;
$menu_item->title = get_post_meta( $menu_item->ID, '_menu_item_title', true );
return $menu_item;
endif;
add_filter( 'wp_get_nav_menu_items', 'custom_generate_menu_from_items', null, 3 );
}
With this code, the menu is showed in output page but the pages are placed in the same level, without parent child relationship.
How can I keep this relationship?
Thank you your support.
There is no logic in there that checks the relationships, I'd say
var_dump($items);
somewhere and see if there is some field pertaining to the relationships of the menu items. Probably something looking like "parentId" or along those lines.
If it's not there, then you'll have to query them yourself.
Once you have them, you can check if it has a parentId, and handle the formatting that way. This will get a little tricky if it nest down more than one level, because you'd have to start checking if the parent has a parent and handle the formatting for all those different cases
I am not using the nivo slider wordpress plugin, instead I am using regular nivo slider jquery and implemented it to work in wordpress, and currently my "read more" button is as follows:
<a href='".get_permalink()."'>Read More</a>
What I want to implement is something like a get_permalinkpage? So basically I want to be able to make the read more link to a wordpress page of my choosing instead of the post's permalink. But I do not know how to implement a custom option to the posts page that would allow the user to say "Choose from pages to link the nivo slider slide to: (then shows pages on website)" and then output that selection.
Any help? This is the last thing I need to implement for our website!
Right, I have your answer here as it's something I did myself recently. This is really a question about custom metaboxes. Here's some resources on it - I got sent a link on it from a mate who recommends this;
http://www.deluxeblogtips.com/meta-box/
And in the Bones theme the author recommends this one;
https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
I will post some code here if you want to quickly get going with it. I would place the following code in a file in it's own and include it from your functions.php, ie;
require_once('includes/metabox-post.php');
Create an includes directory in your theme directory and create a file containing this code;
<?php
/* CUSTOM METABOX -----------------------------------------------------*/
//We create an array called $meta_box and set the array key to the relevant post type
$meta_box_post['post'] = array(
//This is the id applied to the meta box
'id' => 'post-format-meta',
//This is the title that appears on the meta box container
'title' => 'My Custom Metabox',
//This defines the part of the page where the edit screen section should be shown
'context' => 'normal',
//This sets the priority within the context where the boxes should show
'priority' => 'high',
//Here we define all the fields we want in the meta box
'fields' => array(
array(
'name' => 'Home Slider Link',
'desc' => 'You can create a custom link for the home slider image (ie to link to the shop). If left blank, it will by default link through to this post.',
'id' => 'home-slide-link',
'type' => 'text',
'default' => ''
)
)
);
add_action('admin_menu', 'meta_add_box_post');
//Add meta boxes to post types
function meta_add_box_post() {
global $meta_box_post;
foreach($meta_box_post as $post_type => $value) {
add_meta_box($value['id'], $value['title'], 'meta_format_box_post', $post_type, $value['context'], $value['priority']);
}
}
//Format meta boxes
function meta_format_box_post() {
global $meta_box_post, $post;
// Use nonce for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
break;
case 'select':
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':
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':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' /<br /> '. $field['desc'];
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
// Save data from meta box
function meta_save_data_post($post_id) {
global $meta_box_post, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
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 ($meta_box_post[$post->post_type]['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);
}
}
}
add_action('save_post', 'meta_save_data_post');
?>
The this will add a new custom metabox to your posts which you can type in an alternative url into. This custom option will have the id home-slide-link. To use that url you would include the following in your template loop whilst building up the list of Nivoslider images;
<?php
if ( get_post_meta($post->ID, 'home-slide-link', true) ) :
$slideLink = get_post_meta($post->ID, 'home-slide-link', true);
else :
$slideLink = get_permalink();
endif;
echo '<img src="image link in here" />';
?>
So if the post has a url set for the slider link then it uses that, if not it defaults to the permalink.
Hope this helps you a bit!
Here's my solution based on yours.
<div id="slider">
<?php
$captions = array();
$tmp = $wp_query;
$wp_query = new WP_Query('cat='.$category.'&posts_per_page=$n_slices' );
if($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post();
$captions[] = '<p>'.get_the_title($post->ID).'</p><p>'.get_the_excerpt().'</p>';
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'nivothumb');
$mykey_values = get_post_custom_values('home-slide-link');
?>
<a href="<?php echo $mykey_values[0] ?>">
<img src="<?php echo $image[0]; ?>" class="attachment-nivothumb wp-post-image" title="#caption<?php echo count($captions)-1; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
<?php
endwhile;
endif;
$wp_query = $tmp;
?>
</div><!-- close #slider -->
<?php
foreach($captions as $key => $caption) :
?>
<div id="caption<?php echo $key; ?>" class="nivo-html-caption">
<?php echo $caption;?>
</div>
<?php
endforeach;
?>
I can't find my attached image in custom post type.
The code in functions.php
$prefix = 'custom_';
$custom_meta_fields = array(
array(
//'label' => 'Textarea',
'desc' => 'A description for the field.',
'id' => $prefix.'textarea',
'type' => 'textarea'
)
);
// Default metabox for custom post types.
function avz_custom_meta_box() {
// $post_types = get_post_types( array( 'public' => true ) );
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
if ( $post_type == 'page' || $post_type =='post' )
continue;
add_meta_box(
$prefix.'image',
'Header Image Upload Box',
'show_avz_custom_meta_box',
$post_type,
'normal',
'high' );
}
}
add_action('add_meta_boxes', 'avz_custom_meta_box');
function show_avz_custom_meta_box() {
global $avz_custom_meta_box_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="avz_custom_meta_box_fields_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($avz_custom_meta_box_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<td>';
switch($field['type']) {
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> Remove Image</small>
<br clear="all" /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_multiBox_custom_meta($post_id) {
global $avz_custom_meta_box_fields;
// verify nonce
if (!wp_verify_nonce($_POST['avz_custom_meta_box_fields_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
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;
}
// loop through fields and save the data
foreach ($avz_custom_meta_box_fields as $field) {
if($field['type'] == 'tax_select') continue;
$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);
}
} // enf foreach
// save taxonomies
$post = get_post($post_id);
$category = $_POST['category'];
wp_set_object_terms( $post_id, $category, 'category' );
}
add_action('save_post', 'save_multiBox_custom_meta');
And in single.php
if( $image_upload_id = get_post_meta($post->ID, $field['custom_image'], true)){
$img = $image_upload_id ['custom_image'][0];
echo wp_get_attachment_image($img, 'full');
}
But can't find attached image.
In admin uploaded image show but in post page not showing.
Have you tried to access the post attachments? just to see if its there?
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
echo "<pre>";
print_r($attachments);
echo "</pre>";
$attachment = $attachments[0];
// print full link to attachment
the_attachment_link($attachment->ID, false);
}else{
echo "No Attachments for this post!";
}
?>
im not able to save post data from my custom metabox
this is my code
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['args']['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>';
}
and this is the code for save post data
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);
}
}
}
where is the error?
Thanks in advance!
In case it's not a mere omission of your part, you need to hook ri_meta_save_data into something, e.g. the save_post action.
Thanks for your replies,
anyway i had already added this line
add_action('save_post', array(&$this,'ri_meta_save_data'));
this is my function :
$prefix = 'dbt_';
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Custom meta box',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Checkbox',
'id' => $prefix . 'checkbox',
'type' => 'checkbox'
)
)
);
add_action('admin_menu', 'mytheme_add_box');
// Add meta box
function mytheme_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'mytheme_save_data');
// Save data from meta box
function mytheme_save_data($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
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 ($meta_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);
}
}
}
i try to use this code in my template if check box checked :
<?php if ($meta_box = get_post_meta($post->ID, "checkbox", true) ) : ?>
Show this when checkbox checked
<?php endif; ?>
And it doesnt work
You should have your answer by now, but this line should give you a clue:
'id' => $prefix . 'checkbox'
So changing your if statement to:
$meta_box = get_post_meta($post->ID, dbt_checkbox, true)
should help you out.
Just try this code
<?php
add_action( 'admin_init', 'my_admin_samplepost' );
function my_admin_samplepost() {
add_meta_box( 'samplepost_meta_box', 'Car Details', 'display_samplepost_meta_box','samplepost', 'normal', 'high' );
}
function display_samplepost_meta_box( $samplepost ) {
?>
<h4>General Details</h4>
<table width="100%">
<tr>
<td style="width: 25%">Monthly Paymeny</td>
<td><input type="text" style="width:425px;" name="meta[payment]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'payment', true ) );?>" />
</td>
</tr>
<tr>
<td>Price ($)</td>
<td><input type="text" style="width:425px;" name="meta[price]" placeholder="$" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'price', true ) );?>" />
</td>
</tr>
<tr>
<td>Milage</td>
<td><input type="text" style="width:425px;" name="meta[milage]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'milage', true ) );?>" />
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'add_samplepost_fields', 10, 2 );
function add_samplepost_fields( $samplepost_id, $samplepost ) {
if ( $samplepost->post_type == 'samplepost' ) {
if ( isset( $_POST['meta'] ) ) {
foreach( $_POST['meta'] as $key => $value ){
update_post_meta( $samplepost_id, $key, $value );
}
}
}
}