I see it everywhere: in single post, in category archives etc, but in front page post not.
I display the metaboxes in this way:
<?php if (get_post_meta($post->ID, 'test_1', true) != '' ) { ?><?php echo get_post_meta($post->ID, 'test_1', true);?><?php } ?>
What's wrong?
What i'm doing wrong?
Is possible to display the meta boxies in a different way?
Here is the code:
add_action( 'init', 'create_xy_taxonomies', 0 );
function create_pc_db_taxonomies() {
register_taxonomy( 'genre', 'post', array( 'hierarchical' => false, 'label' => __('Tests', 'series'), 'query_var' => 'test', 'rewrite' => array( 'slug' => 'tests' ) ) );
$prefix = 'xy_meta_';
$meta_box = array(
'id' => 'xy-meta-box',
'title' => 'Xy tests',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Test_1',
'id' => 'test_1',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Test_2',
'id' => 'test_2',
'type' => 'select',
'options' => array('','1'),
'std' => ''
),
)
);
add_action('admin_menu', 'mytheme_add_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']);
}
function mytheme_show_box() {
global $meta_box, $post;
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) {
$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['std'], '" 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['std'], '</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"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'mytheme_save_data');
function mytheme_save_data($post_id) {
global $meta_box;
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
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);
}
}
}
Your test would only display the content of the custom field key test_1, yeah?
Related
time ago, I found this code for custom meta boxes. It works well, but I'd like to add an image uploader. Since I can't remember where I found this code - and thus contact the author - is there anyone who can help me solve this issue?
$meta_boxes = array();
// Description
$meta_boxes[] = array(
'id' => 'descriptions',
'title' => 'Description',
'pages' => array('fleet', 'link'), // multiple post types
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Title',
'desc' => 'Insert a title here',
'id' => 'desc-title',
'type' => 'text',
'std' => 'Some problem'
),
)
);
foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}
class My_meta_box {
protected $_meta_box;
// create meta box based on given data
function __construct($meta_box) {
$this->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'],
array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div>';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<label for="', $field['id'], '">', $field['name'], '</label><br>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-side':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:100%; height:25px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-normal':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:100%; height:300px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>',
'<br />', $field['desc'], '<br />';
break;
case 'textarea-half':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="'. $field['std'] .'" cols="60" rows="4" style="width:50%; height:250px; border:1px solid black;box-sizing:border-box;">'. get_post_meta($post->ID, $field['id'], true) .'</textarea>';
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 />';
break;
}
}
echo '</div>';
}
// Save data from meta box
function save($post_id) {
// 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 ($this->_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);
}
}
}
}
Many thanks.
I'm getting this error with my custom post metabox. The custom post and metabox works fine, but if I have debug on and when I try to save any other page or post, I will get an error:
Notice: Undefined index: iam_video_select in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_video_url in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_link in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
My code:
<?php
$meta_boxes = array();
//Video meta box
$meta_boxes[] = array(
'id' => 'video-meta-box',
'title' => __( 'Video', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Select Video Host',
'id' => PTHEME . '_video_select',
'type' => 'select',
'options' => array( 'Youtube', 'Vimeo', 'Self hosted' )
),
array(
'name' => 'URL',
'desc' => 'Enter video url here.',
'id' => PTHEME . '_video_url',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
//Link meta box
$meta_boxes[] = array(
'id' => 'link-meta-box',
'title' => __( 'Link', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Link',
'desc' => 'Enter your url here.',
'id' => PTHEME . '_link',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
/**
*
*/
class Custom_Meta_Boxes{
public $_meta_box;
public function __construct( $meta_box ){
$this->_meta_box = $meta_box;
add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );
}
/**
* Adds a meta box to the post editing screen
*/
public function iam_add_meta_box(){
add_meta_box(
$this->_meta_box['id'],
$this->_meta_box['title'],
array( &$this, 'iam_display_custom_meta_box' ),
$this->_meta_box['pages'],
$this->_meta_box['context'],
$this->_meta_box['priority']
);
}
/**
* Render Meta Box content.
*/
public function iam_display_custom_meta_box() {
global $post;
// Add an nonce field so we can check for it later.
wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );
echo '<div class="metabox-wrapper">';
foreach ( $this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta( $post->ID, $field['id'], true );
echo '<div class="metabox-fields metabox_' , $field['type'] , '">';
echo '<label for="', $field['id'] , '">', $field['name'] , '</label>';
switch ( $field['type'] ) {
case 'text':
echo '<input type="text" name="', $field['id'] , '" id="', $field['id'] , '" value="', $meta , '" />';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta , '</textarea>';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
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"' : '', ' />';
break;
}
echo '</div>';
}
echo '</div>';
}
/**
* Save the meta when the post is saved.
*/
public function iam_save_meta_box_data( $post_id ){
if( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ) {
// Checks for input and sanitizes/saves if needed
foreach ( $this->_meta_box['fields'] as $field ) {
$old = get_post_meta( $post_id, $field['id'], true );
$new = sanitize_text_field( $_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 );
}
}
}
}
/**
* Determines whether or not the current user has the ability to save meta
* data associated with this post.
*
* #param int $post_id The ID of the post being save
* #param bool Whether or not the user has the ability to save this post.
*/
public function iam_user_can_save( $post_id, $nonce ){
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';
// Return true if the user is able to save; otherwise, false.
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
}
// Instantiate theme
if ( class_exists( 'Custom_Meta_Boxes' ) ){
foreach ( $meta_boxes as $meta_box ) {
$my_box = new Custom_Meta_Boxes( $meta_box );
}
}
?>
Thanks for any helps.
I have a Wordpress plugin that shows a ratting snippet for Google. 1.0 - 5.0 and I would like to add a image for the value selected, right after the rating, but it doesn't.
So far I have got it to load the image but it doesn't show up where I want it to, and that is where I am stuck.
Here is was I tried to add:
if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");}
here is the plugin code:
<?php
$prefix = 'pk_rs_';
DEFINE ('PK_RS_DEFAULT_RATING', '-');
DEFINE ('PK_RS_DISPLAY', true);
$pk_rs_meta_box = array(
'id' => 'pk_rich_snippet_review',
'title' => 'Google Rich Snippets: Reviews',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Rating',
'desc' => 'Product rating, from 1 to 5.',
'id' => 'rating',
'type' => 'select',
'options' => array('-', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0')
),
array(
'name' => 'Author',
'desc' => 'Author display name.',
'id' => 'author',
'type' => 'text',
'std' => ''
)
)
);
add_action('admin_menu', 'pk_rich_snippet_add_box');
// Add meta box
function pk_rich_snippet_add_box() {
global $pk_rs_meta_box;
add_meta_box($pk_rs_meta_box['id'], $pk_rs_meta_box['title'], 'pk_rich_snippet_show_box', $pk_rs_meta_box['page'], $pk_rs_meta_box['context'], $pk_rs_meta_box['priority']);
}
// Callback function to show fields in meta box
function pk_rich_snippet_show_box() {
global $pk_rs_meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="pk_rich_snippet_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($pk_rs_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 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" 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['std'], '</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>', '<br />', $field['desc'];
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>';
}
add_action('save_post', 'pk_rs_save_data');
// Save data from meta box
function pk_rs_save_data($post_id) {
global $pk_rs_meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['pk_rich_snippet_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 ($pk_rs_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);
}
}
}
add_filter('the_content', 'pk_rs_add_rich_snippet_to_content', 20);
function pk_rs_add_rich_snippet_to_content($content){
if (is_single()&&!is_feed()){
global $post;
$rating = get_post_meta($post->ID, 'rating', true);
$rating = ( '-' == $rating && '-' != PK_RS_DEFAULT_RATING ) ? PK_RS_DEFAULT_RATING : $rating;
if ( '-' != $rating ){
$title = $post->post_title ;
$dateTime = date_create( $post->post_date );
$date = $dateTime->format("Y-m-d");
$date_only = $dateTime->format("M j");
$author = get_post_meta($post->ID, 'author', true);
$author = ( '' == $author ) ? ucfirst(get_the_author_meta('display_name', $post->post_author)) : $author;
$summary = get_post_meta($post->ID, 'summary', true);
$description = get_post_meta($post->ID, 'description', true);
if ( !PK_RS_DISPLAY ) {
$output = "<div class=\"hreview\" style=\"display:none\">";
$output .= "<span class=\"item\"><span class=\"fn entry-title\">".$title."</span></span>";
$output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span>";
$output .= "Rating: <span class=\"rating\">".$rating."</span>";
$output .= "<span class=\"summary\">".$summary."</span>";
$output .= "<span class=\"description\">".$description."</span>";
$output .= "</div>";
} else {
$output = "<div class=\"hreview\" style=\"display:block; margin: 0 0 10px 10px; padding: 10px; background: #F6F6F6; border: 1px solid #DDD; -moz-border-radius: 3px; border-radius: 3px; font-size: 0.8em; width: 30%; float: right;\">";
$output .= "Title: <span class=\"item\"><span class=\"fn entry-title\" style=\"font-size: 0.8em;font-weight: normal;\">".$title."</span></span><br />";
$output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span><br/>";
$output .= "Rating: <span class=\"rating\">".$rating."</span><br/>";
$output .= ( 0 < strlen($summary) ) ? "Summary: <span class=\"summary\">".$summary."</span><br/>" : "";
$output .= ( 0 < strlen($description) ) ? "<p><span class=\"description\">".$description."</span></p>" : "";
$output .= "</div>";
}
$content = $output.$content ;
}
}
return $content;
}
?>
I'd do something like:
$imgTag = "<img src = 'stars/$rating.png' />";
And then modify the line that says:
$output .= "Rating: <span class=\"rating\">".$rating."</span><br/>";
To something like:
$output .= "Rating: <span class=\"rating\">".$rating. $imgTag . "</span><br/>";
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 );
}
}
}
}