Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks in advance.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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>';
}
share|improve this answer
    
So much functions with namespace, maybe to use a class, Singleton for easier to read. –  bueltge Dec 28 '12 at 21:16

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

share|improve this answer
    
Absolutely right. The two solutions work. But now, i am looking to short the ID's at the first column and not in the last column. –  MikeD Dec 28 '12 at 18:37
    
The solution is already here, I'm testing @toscho code (brilliant, btw), and it's just a matter of adapting the function t5_add_col to include the wpse_77532_array_push_after logic. –  brasofilo Dec 28 '12 at 18:40
    
I try to include this logic, but i don't have the result that i want. –  MikeD Dec 28 '12 at 18:48
    
So much functions with namespace, maybe to use a class, Singleton for easier to read. –  bueltge Dec 28 '12 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.