wordpress plugin - How do you add custom columns for these custom post types? -
i've been developing plugin involves custom post types , taxonomies. i'm struggling @ moment getting custom post fields display in 'all reservations' screen. can columns display, cannot life of me fields within columns populate data database.
my custom post code below:
function reservations_init() { $labels = array( 'name' => _x( 'reservations', 'post type general name' ), 'singular_name' => _x( 'reservation', 'post type singular name' ), 'add_new' => _x( 'add new', 'reservation' ), 'add_new_item' => __( 'add new reservation' ), 'edit_item' => __( 'edit reservation' ), 'new_item' => __( 'new reservation' ), 'all_items' => __( 'reservations' ), 'view_item' => __( 'view reservation' ), 'search_items' => __( 'search reservations' ), 'not_found' => __( 'no reservations found' ), 'not_found_in_trash' => __( 'no reservations found in trash' ), 'parent_item_colon' => '', 'menu_name' => 'reservations' ); $args = array( 'labels' => $labels, 'description' => 'holds our reservations', 'public' => true, 'menu_position' => 5, 'supports' => array( 'none' ), 'has_archive' => true, 'show_in_nav_menu' => true, 'show_in_menu' => 'edit.php?post_type=meals', ); register_post_type( 'reservations', $args ); } add_action( 'init', 'reservations_init' ); function restauranter_add_meta_box() { $screens = array( 'reservations' ); foreach ( $screens $screen ) { add_meta_box( 'restauranter_firstname', __( 'reservation details', 'restauranter' ), 'restauranter_reservation_details', $screen, 'normal', 'high' ); } } add_action( 'add_meta_boxes', 'restauranter_add_meta_box' ); function restauranter_reservation_details() { global $post; echo '<input type="hidden" name="reservationmeta_noncename" id="reservationmeta_noncename" value="' . wp_create_nonce( plugin_basename(__file__) ) . '" />'; $firstname = get_post_meta($post->id, '_firstname', true); $lastname = get_post_meta($post->id, '_lastname', true); $phone = get_post_meta($post->id, '_phone', true); $email = get_post_meta($post->id, '_email', true); $guests = get_post_meta($post->id, '_guests', true); $date = get_post_meta($post->id, '_date', true); $time = get_post_meta($post->id, '_time', true); echo '<p>first name:</p>'; echo '<input type="text" name="_firstname" value="' . $firstname . '" class="widefat" />'; echo '<p>last name:</p>'; echo '<input type="text" name="_lastname" value="' . $lastname . '" class="widefat" />'; echo '<p>phone number:</p>'; echo '<input type="text" name="_phone" value="' . $phone . '" class="widefat" />'; echo '<p>email:</p>'; echo '<input type="text" name="_email" value="' . $email . '" class="widefat" />'; echo '<p>number of guests:</p>'; echo '<input type="text" name="_guests" value="' . $guests . '" class="widefat" />'; echo '<p>reservation date:</p>'; echo '<input type="text" name="_date" value="' . $date . '" class="widefat date" />'; echo '<p>reservation time:</p>'; echo '<input type="text" name="_time" value="' . $time . '" class="widefat time" />'; echo '<input type="submit" name="_time" value="" class="widefat submit" />'; } function restauranter_save_reservations_meta($post_id, $post) { if ( !wp_verify_nonce( $_post['reservationmeta_noncename'], plugin_basename(__file__) )) { return $post->id; } // user allowed edit post or page? if ( !current_user_can( 'edit_post', $post->id )) return $post->id; $reservations_meta['_firstname'] = $_post['_firstname']; $reservations_meta['_lastname'] = $_post['_lastname']; $reservations_meta['_phone'] = $_post['_phone']; $reservations_meta['_email'] = $_post['_email']; $reservations_meta['_guests'] = $_post['_guests']; $reservations_meta['_date'] = $_post['_date']; $reservations_meta['_time'] = $_post['_time']; foreach ($reservations_meta $key => $value) { if( $post->post_type == 'revision' ) return; $value = implode(',', (array)$value); if(get_post_meta($post->id, $key, false)) { update_post_meta($post->id, $key, $value); } else { add_post_meta($post->id, $key, $value); } if(!$value) delete_post_meta($post->id, $key); } } add_action('save_post', 'restauranter_save_reservations_meta', 1, 2); // save custom fields function add_new_reservations_columns($reservations_columns) { $new_columns['cb'] = '<input type="checkbox" />'; $new_columns['_firstname'] = __('first name', '_firstname'); return $new_columns; } add_filter('manage_edit-reservations_columns' , 'add_new_reservations_columns');
i have tried googling, have tried numerous samples of code, have tried playing code self still no joy.
you need 1 more function add data columns....
function custom_edit-reservations_columns( $column, $post_id ) { switch ( $column ) { case "_firstname": $firstname = get_post_meta($post_id, '_firstname', true); echo $firstname; break; } } add_action( "manage_posts_custom_column", "custom_edit-reservations_columns", 10, 2 );
hope helps
Comments
Post a Comment