here’s the complete code : if somebody could try it, i might doing something wrong, but i can’t find out what. (the whole code is inside a widget page)
/*
*
* add meta box : habillage
*
*/
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
add_action( 'save_post', 'myplugin_save_postdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
'test1',
__( 'My test1', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'post'
);
add_meta_box(
'test2',
__( 'My test2', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'movies'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
$met = get_post_meta($post->ID, 'champ', true)? get_post_meta($post->ID, 'champ', true) : 'empty meta';
/* input */
echo '<label for="myplugin_new_field">';
_e("Description field : ", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="champ" name="champ" value="'.$met.'" />';
/* select list */
echo '<label for="my_list_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<select name="my_list_field" id="my_list_field">';
print_r ($post->ID);
global $post;
$s_query = new WP_Query( array(
'suppress_filters' => false,
'post_type' => 'movies'));
while($s_query->have_posts()):$s_query->the_post();
$sname = $post->post_title;
$s_output2 ='';
$s_output2 .= '<option value="'.$post->ID.'" >';
$s_output2 .= $post->post_title.' : allo';
$s_output2 .= '</option>';
echo $s_output2;
endwhile ;
echo '</select>';
wp_reset_query();
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( 'page' == $_POST['post_type'] )
{
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else
{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
// OK, we're authenticated: we need to find and save the data
//$mydata = $_POST['myplugin_new_field'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta()
add_post_meta($post->ID, "champ_key", 'banana');
return $post_id;
//return $mydata;
}
?>