Custom columns

Support MB Admin Columns Custom columns

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3731
    nicolasnicolas
    Participant

    Is is possible to use the Admin Columns class to create custom columns without creating any custom fields?

    #3735
    Anh TranAnh Tran
    Keymaster

    Yes, but that requires some custom code. Here is what I tried:

    - In your plugin file or theme's functions.php add this code:

    add_action( 'admin_init', 'prefix_custom_post_column', 20 );
    function prefix_custom_post_column() {
    	require_once 'custom.php';
    	new Test_Admin_Columns( 'post', array() );
    }

    - Create a new file custom.php and enter the following code:

    class Test_Admin_Columns extends MB_Admin_Columns_Post {
    	public function columns( $columns ) {
    		$columns = parent::columns( $columns );
    
    		/**
    		 * Add more column in a specific position
    		 *
    		 * @param string $position New column position. Empty to not specify the position. Could be 'before', 'after' or 'replace'
    		 * @param string $target The target column. Used with combination with $position
    		 */
    		$position = '';
    		$target   = '';
    		$this->add( $columns, 'column_id', 'Column Title', $position, $target );
    
    		// Add more if you want
    
    		return $columns;
    	}
    
    	public function show( $column, $post_id ) {
    		switch ( $column ) {
    			case 'column_id':
    				echo 'Column content';
    				break;
    			// More columns
    		}
    	}
    }

    Note that the call new Test_Admin_Columns( 'post', array() ); is specific for post. You can change it to any custom post type if you want.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Custom columns’ is closed to new replies.