WP Add Column to Admin Post Type Table

<?php $__column = array(); function add_cpt_column( $cpt, $column_data ){ global $__column; // insert cpt data do global column object $__column[ $cpt ] = array_merge( array( 'id' => '', 'name' => '', 'content' => '', 'position' => '', ), $column_data ); // add column to be displayed add_filter( 'manage_'. $cpt .'_posts_columns', function( $defaults ){ global $__column; $_column = $__column[ $_GET[ 'post_type' ] ]; if( $_column[ 'position' ] ){ $c = 0; $cols = array(); foreach( $defaults as $id => $name ){ if( $c == $_column[ 'position' ] ){ $cols[ $_column[ 'id' ] ] = $_column[ 'name' ]; } $cols[ $id ] = $name; $c++; } $defaults = $cols; } else { $defaults[ $_column[ 'id' ] ] = $_column[ 'name' ]; } return $defaults; }, 10 ); // show column content add_action( 'manage_'. $cpt .'_posts_custom_column', function( $col_name, $postID ){ global $__column; $_column = $__column[ $_GET[ 'post_type' ] ]; if( $col_name == $_column[ 'id' ] ){ echo str_replace( array( '{id}', '{title}' ), array( $postID, get_the_title( $postID ) ), $_column[ 'content' ] ); } }, 10, 2 ); } /*** * How to use * * add_cpt_column( 'movie', array( 'id' => 'sc', 'name' => 'Shortcode', 'content' => '[movie id="{id}" title="{title}"]', 'position' => 2, * ) ); * * it should display the respective shortcode after the ID (0) and TITLE (1) columns * /
Add custom column and custom content for Custom Post Type.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.