.

CPT Custom Fields, , , :
<?php
class SO_25936506
{
private $cpt = 'reward';
public function __construct()
{
add_action( 'init', array( $this, 'init' ) );
add_action( 'save_post', array( $this, 'save' ), 10, 2 );
}
public function init() {
$labels = array(
'name' => _x( 'Rewards', 'post type general name' ),
'singular_name' => _x( 'Reward', 'post type singular name' ),
'add_new' => _x( 'Add New', 'reward' ),
'add_new_item' => __( 'Add New Reward' ),
'edit_item' => __( 'Edit Reward' ),
'new_item' => __( 'New Reward' ),
'all_items' => __( 'All Rewards' ),
'view_item' => __( 'View Reward' ),
'search_items' => __( 'Search Rewards' ),
'not_found' => __( 'No rewards found' ),
'not_found_in_trash' => __( 'No rewards found in Trash' ),
'parent_item_colon' => '',
'menu_name' => __( 'Rewards' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => _x( 'reward', 'URL slug' ) ),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'taxonomies' => array('prize'),
'supports' => array( 'title', 'editor', 'thumbnail' ),
'register_meta_box_cb' => array( $this, 'add_metabox' )
);
register_post_type( $this->cpt, $args );
register_taxonomy('prize', $this->cpt, array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Prizes', 'taxonomy general name' ),
'singular_name' => _x( 'Prize', 'taxonomy singular name' ),
'search_items' => __( 'Search Prizes' ),
'all_items' => __( 'All Prizes' ),
'parent_item' => __( 'Parent Prize' ),
'parent_item_colon' => __( 'Parent Prize:' ),
'edit_item' => __( 'Edit Prize' ),
'update_item' => __( 'Update Prize' ),
'add_new_item' => __( 'Add New Prize' ),
'new_item_name' => __( 'New Prize Name' ),
'menu_name' => __( 'Prizes' ),
),
'rewrite' => array(
'slug' => 'prizes',
'with_front' => false,
'hierarchical' => true
),
));
}
public function add_metabox()
{
add_meta_box(
'winners',
'Winners',
array( $this, 'do_metabox' ),
$this->cpt,
'normal',
'default',
array(
'_winner_gold' => array( 'title'=>'Gold', 'desc' => 'G desc' ),
'_winner_silver' => array( 'title'=>'Silver', 'desc' => desc' ),
'_winner_bronze' => array( 'title'=>'Bronze', 'desc' => 'B desc' )
)
);
}
// back function of add meta box that displays the meta box in the post edit screen
public function do_metabox( $post, $box )
{
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_so_25936506' );
foreach( $box['args'] as $field => $value )
$this->print_field( $field, $value, $post->ID );
}
public function print_field( $field, $value, $post_id )
{
$post_meta = get_post_meta( $post_id, $field, true);
$selected = ($post_meta) ? $post_meta : false;
$users_dd = wp_dropdown_users(array(
'name' => 'author',
'echo'=>false,
'name'=>$field,
'show_option_none'=>'Select winners'
));
printf(
'<label>%s: </label>%s <small>%s</small><br/>',
$value['title'],
$users_dd,
$value['desc']
);
}
public function save( $post_id, $post_object )
{
// Verify auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Security
if (
!isset( $_POST['noncename_so_25936506'] )
|| !wp_verify_nonce( $_POST['noncename_so_25936506'], plugin_basename( __FILE__ ) )
)
return;
if ( $this->cpt !== $post_object->post_type )
return;
// Process post data
foreach( array('_winner_gold','_winner_silver','_winner_bronze') as $field )
{
if ( isset( $_POST[$field] ) )
update_post_meta( $post_id, $field, $_POST[$field] );
else
delete_post_meta( $post_id, $field );
}
}
}
new SO_25936506();