How to append custom columns to post types

22:50
wordpress allows us to append custom columns  to post types by using action hook so first we are creating custom post types.

Register Custom Post Types

please copy below code in your functions.php or custom plugin
add_action( 'init', 'coupon_setup_post_type');
function coupon_setup_post_type() {
 /* coupon type */
    $labels = array(
        'name'                  => _x( 'Coupons', 'Post type general name', 'textdomain' ),
        'singular_name'         => _x( 'Coupon', 'Post type singular name', 'textdomain' ),
        'menu_name'             => _x( 'Coupons', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'        => _x( 'Coupon', 'Add New on Toolbar', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New Coupon', 'textdomain' ),
        'new_item'              => __( 'New Coupon', 'textdomain' ),
        'edit_item'             => __( 'Edit Coupon', 'textdomain' ),
        'view_item'             => __( 'View Coupon', 'textdomain' ),
        'all_items'             => __( 'All Coupons', 'textdomain' ),
        'search_items'          => __( 'Search Coupons', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent Coupons:', 'textdomain' ),
        'not_found'             => __( 'No books found.', 'textdomain' ),
        'not_found_in_trash'    => __( 'No books found in Trash.', 'textdomain' ),
        'featured_image'        => _x( 'Coupon Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'              => _x( 'Coupon archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'      => _x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'     => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation' => _x( 'Coupons list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'            => _x( 'Coupons list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'coupon' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor','thumbnail'),
    );
 
    register_post_type( 'coupon', $args );
}
if adding above code in your plugin then register new custom post types (coupons) in your admin dashboard.

Append Custom Column Headers

we are overriding the default behavior of the custom post type table headers by using following hook
add_filter( 'manage_edit-coupon_columns','coupon_columns');
function coupon_columns( $columns ) {
 
  if ( ! is_array( $columns ) ) {
   $columns = array();
  }

  unset( $columns['date'], $columns['author'] );

  $columns["coupon_posted"]           = __( "Posted", 'custom-coupon' );
  $columns["coupon_start"]           = __( "Start", 'custom-coupon' );
  $columns["coupon_expires"]          = __( "Expires", 'custom-coupon' );
  return $columns;
 }

Append Custom Custom Columns Data

add_action( 'manage_coupon_posts_custom_column', 'custom_coupon_columns');
function custom_coupon_columns( $column ) {
  global $post;

  switch ( $column ) {
   case "coupon_posted" :
    echo '<strong>' . date_i18n( __( 'M j, Y', 'custom-coupon' ), strtotime( $post->post_date ) ) . '</strong><span>&nbsp;';
    echo ( empty( $post->post_author ) ? __( 'by a guest', 'wp-job-manager' ) : sprintf( __( 'by %s', 'custom-coupon' ), '<a href="' . esc_url( add_query_arg( 'author', $post->post_author ) ) . '">' . get_the_author() . '</a>' ) ) . '</span>';
   break;
   case "coupon_expires" :
    $expire = get_post_meta($post->ID,'_coupon_end',true);
    if ( $expire )
     echo '<strong>' . date_i18n( __( 'M j, Y', 'custom-coupon' ), strtotime( $expire ) ) . '</strong>';
    else
     echo '&ndash;';
   break;
   case "coupon_start" :
    $start = get_post_meta($post->ID,'_coupon_start',true);
    if ( $start )
     echo '<strong>' . date_i18n( __( 'M j, Y', 'custom-coupon' ), strtotime( $start ) ) . '</strong>';
    else
     echo '&ndash;';
   break;
  }
 }
 

No comments:

Powered by Blogger.