How to Add a Custom Column for a Post Type in WordPress

WPTurbo » Snippets » How to Add a Custom Column for a Post Type in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to add a custom column for a specific post type in your WordPress site? Customizing the columns in the WordPress admin area can greatly enhance your workflow and make it easier to manage your content. In this article, we’ll show you how to create a custom column for a post type and display relevant information that is important for your site’s admin users.

					function wpturbo_add_custom_column( $columns ) {
    $columns['custom_column'] = __( 'Custom Column', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_post_type_posts_columns', 'wpturbo_add_custom_column' );

function wpturbo_display_custom_column( $column, $post_id ) {
    if ( 'custom_column' === $column ) {
        echo __( 'Custom Content', 'wpturbo' );
    }
}
add_action( 'manage_post_type_posts_custom_column', 'wpturbo_display_custom_column', 10, 2 );
				

The code snippet provided here allows you to add a custom column to the admin area of your WordPress website. This column can be used to display custom content for each post within a specific post type.

First, we need to define a function called wpturbo_add_custom_column(). This function is responsible for adding the custom column to the list of columns for the chosen post type. In the code snippet, we are using the manage_post_type_posts_columns filter to achieve this.

Inside the wpturbo_add_custom_column() function, we add the custom column to the existing $columns array by using the column name as the key and the desired column label as the value. In this case, the column name is ‘custom_column’, and the label is ‘Custom Column’. We use the __() function to make the label translatable.

After adding the custom column label, we return the modified $columns array.

The next step is to define another function called wpturbo_display_custom_column(). This function is responsible for displaying the custom content within the custom column for each post in the chosen post type. We use the manage_post_type_posts_custom_column action to achieve this.

Inside the wpturbo_display_custom_column() function, we check if the current column being rendered is the custom column by comparing the $column parameter to ‘custom_column’. If it is, we echo out the custom content using the __() function to make it translatable. In this example, the custom content is ‘Custom Content’.

Lastly, we hook the wpturbo_display_custom_column() function into the manage_post_type_posts_custom_column action. The 10 parameter specifies the priority of the hook, and the 2 parameter indicates that the function accepts two arguments: $column and $post_id.

Once you add this code to your WordPress website, you should see the custom column with the custom content displayed in the admin area for the chosen post type.

Register an account to save your snippets or go Pro to get more features.