How to add the category ID to admin page

I want to add the categories ID to admin page.
I call it for my functions.php: require_once('includes/categories_custom_id.php');
The part of code:

function categoriesColumnsHeader($columns) {
        $columns['catID'] = __('ID');
        return $columns;
}
add_filter( 'manage_categories_columns', 'categoriesColumnsHeader' );
function categoriesColumnsRow($argument, $columnName, $categoryID){
        if($columnName == 'catID'){
                return $categoryID;
        }
}
add_filter( 'manage_categories_custom_column', 'categoriesColumnsRow', 10, 3 );

But it doesn’t work. Any idea, how to do that?

Read More

Thanks in advance.

Related posts

Leave a Reply

2 comments

  1. The hooks for taxonomies are:

    • "manage_edit-${taxonomy}_columns" for the header
    • "manage_edit-${taxonomy}_sortable_columns" to make columns sortable
    • "manage_${taxonomy}_custom_column" for the cell content

    To catch all taxonomies write:

    foreach ( get_taxonomies() as $taxonomy ) {
        add_action( "manage_edit-${taxonomy}_columns",          't5_add_col' );
        add_filter( "manage_edit-${taxonomy}_sortable_columns", 't5_add_col' );
        add_filter( "manage_${taxonomy}_custom_column",         't5_show_id', 10, 3 );
    }
    add_action( 'admin_print_styles-edit-tags.php', 't5_tax_id_style' );
    
    function t5_add_col( $columns )
    {
        return $columns + array ( 'tax_id' => 'ID' );
    }
    function t5_show_id( $v, $name, $id )
    {    
        return 'tax_id' === $name ? $id : $v;
    }
    function t5_tax_id_style()
    {
        print '<style>#tax_id{width:4em}</style>';
    }
    
  2. You had it almost all right, but the hook names, where did you get those from?

    The following are the correct ones. I’m adding two extra functions, one will add our column as the first one (instead of being the last, I guess it makes more sense for an ID column). And the second is a simple CSS fix for the column width.

    Code based in this Q&A: Multisite – Protect categories from deletion?

    add_filter( 'manage_edit-category_columns', 'wpse_77532_cat_edit_columns' );
    add_filter( 'manage_category_custom_column', 'wpse_77532_cat_custom_columns', 10, 3 );
    add_action( 'admin_head-edit-tags.php', 'wpse_77532_column_width' );
    
    /**
     * Register the ID column
     */
    function wpse_77532_cat_edit_columns( $columns )
    {
        $in = array( "cat_id" => "ID" );
        $columns = wpse_77532_array_push_after( $columns, $in, 0 );
        return $columns;
    }   
    
    /**
     * Print the ID column
     */
    function wpse_77532_cat_custom_columns( $value, $name, $cat_id )
    {
        if( 'cat_id' == $name ) 
            echo $cat_id;
    }
    
    /**
     * CSS to reduce the column width
     */
    function wpse_77532_column_width()
    {
        // Tags page, exit earlier
        if( 'category' != $_GET['taxonomy'] )
            return;
    
        echo '<style>.column-cat_id {width:3%}</style>';
    }
    
    /**
     * Insert an element at the beggining of the array
     */
    function wpse_77532_array_push_after( $src, $in, $pos )
    {
        if ( is_int( $pos ) )
            $R = array_merge( array_slice( $src, 0, $pos + 1 ), $in, array_slice( $src, $pos + 1 ) );
        else
        {
            foreach ( $src as $k => $v )
            {
                $R[$k] = $v;
                if ( $k == $pos )
                    $R       = array_merge( $R, $in );
            }
        }
        return $R;
    }
    

    Result:

    id column for categories