How to Create Custom Terms Admin Column in WordPress

Home » Snippets » How to Create Custom Terms Admin Column in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

If you’ve ever worked with custom taxonomies in WordPress, you know that the admin columns for these taxonomies are usually quite limited. By default, WordPress only displays the name, slug, and number of terms for each taxonomy in its admin column. But what if you want to display additional information, such as custom metadata or a featured image? In this article, we’ll show you how to create your own custom admin columns for taxonomies in WordPress, so you can display any information you want.

					function wpturbo_add_custom_terms_column( $columns ) {
    $columns['custom_terms'] = __( 'Custom Terms', 'wpturbo' );
    return $columns;
add_filter( 'manage_edit-category_columns', 'wpturbo_add_custom_terms_column' );

function wpturbo_display_custom_terms_column( $content, $column_name, $term_id ) {
    if ( 'custom_terms' == $column_name ) {
        $term = get_term( $term_id, 'category' );
        $custom_terms = get_term_meta( $term->term_id, 'custom_terms', true );
        $content .=  $custom_terms ? $custom_terms : '-';
    return $content;
add_filter( 'manage_category_custom_column', 'wpturbo_display_custom_terms_column', 10, 3 );

In this tutorial, we will learn how to create a custom terms admin column in WordPress. This column will display additional information for categories, in this case, "Custom Terms". We will be using two functions: wpturbo_add_custom_terms_column() and wpturbo_display_custom_terms_column().

Firstly, the wpturbo_add_custom_terms_column() function is responsible for adding a new column header to the category table. We start by adding a new key-value pair to the $columns array using the custom_terms key. The value in this case is just a translatable string "Custom Terms".

Next, we return the updated $columns array to add the column to the category table. The second argument of the add_filter() function specifies the manage_edit-[taxonomy]_columns hook to use, in this case, manage_edit-category_columns, since we are dealing with categories.

Now, we can see the "Custom Terms" column in our category table. But, the column contains no data! So, we will use the wpturbo_display_custom_terms_column() function to add the appropriate data to the column.

The wpturbo_display_custom_terms_column() function hooks into the manage_[taxonomy]_custom_column action, in this case, manage_category_custom_column. This hook gives us access to the content for each cell in the column.

The function first checks if the $column_name argument is equal to 'custom_terms'. We then retrieve the current category object using get_term(). We pass in the $term_id parameter, which gives us the current term being rendered.

Next, we use get_term_meta(), which takes in the term ID and the meta key as parameters, to retrieve a meta value associated with that term ID. In this case, the meta key is "custom_terms". We store this in the $custom_terms variable.

Finally, we check if $custom_terms has a value and append it to our $content variable. If there is no value, we append a dash ("-") to indicate that there is no data. We return the updated $content variable to update the current cell in the column.

And that’s it! We now have a custom admin column that displays additional information for categories.

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