I followed the example from the book Wordpress Anthology to create my custom field to store data. Most of it works apart the value, I put in the custom field, doesn’t get saved.
Here’s my code:
// Add custom post type for testimonials
function planx_testimonial_register() {
$labels = array(
'name' => __('Testimonials', 'planxt'),
'singular_name' => __('Testimonial', 'planxt'),
'add_new' => __('Add New Testimonial', 'planxt'),
'add_new_item' => __('Add New Testimonial', 'planxt'),
'edit' => __('Edit', 'planxt'),
'edit_item' => __('Edit Testimonial', 'planxt'),
'new_item' => __('New Testimonial', 'planxt'),
'view' => __('View Testimonial', 'planxt'),
'view_item' => __('View Testimonial', 'planxt'),
'search_items' => __('Search Testimonials', 'planxt'),
'not_found' => __('No testimonials', 'planxt'),
'not_found_in_trash' => __('No testimonials in the trash', 'planxt')
);
register_post_type('planx_testimonial', array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'menu_position' => 25,
'menu_icon' => plugins_url('icons/user_comment.png', __FILE__ ),
'rewrite' => array('slug' => 'testimonial'),
'supports' => array( 'title', 'editor'),
'description' => "Testimonials for Alex's hard work",
'exclude_from_search' => true,
'has_archive' => true,
'rewrite' => array('slug' => __('testimonials'))
)
);
}
add_action( 'init', 'planx_testimonial_register' );
// Define custom field's input box
function planx_testimonial_fields(){
global $post;
$custom = get_post_custom($post->ID);
$planx_testimonial_company = $custom["planx_testimonial_company"][0];
?>
<p>
<label><?php _e('Company', 'planxt'); ?></label><br />
<input size="45" name="planx_testimonial_company" value="<?php echo $planx_testimonial_company; ?>" />
</p>
<?php }
//Create meta_box
function add_planx_testimonial_box(){
add_meta_box(
"planx_testimonial_info", // meta_box id
__('Details about person, that gave the testimonial', 'planxt'), //meta_box title
"planx_testimonial_fields", //callback
"planx_testimonial" //custom post type it belongs to
);
}
// Save testimonial attributes
function save_testimonial_attributes(){
global $post;
update_post_meta( $post->ID, "planx_testimonial_company", $POST["planx_testimonial_company"]);
}
add_action( 'admin_init', 'add_planx_testimonial_box' );
add_action( 'save_post', 'save_testimonial_attributes' );
add_action( 'publish_post', 'save_testimonial_attributes' );