johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
979 stars 96 forks source link

$term_id on the register_extended_taxonomy admin_cols #110

Closed oraimbaud closed 6 years ago

oraimbaud commented 6 years ago

Hi, I would like to know if there is a way to display the $term_id within an admin_cols function. I would explain myself probably better with an example:

/**
 * Custom post "book"
 *
 * @package My Theme
 */

add_action( 'init', function() {
    register_extended_taxonomy( 'genre', 'book', array(
        'admin_cols' => array(
            'col_1' => array(
                'title'    => '1',
                'function' => function() {
                    // ?
                },
            ),
        ),
    ));

    register_extended_post_type( 'book' );
});

/**
 * Add taxonomy custom column the default way
 *
 * @param string $columns for the column name.
 */
function add_genre_columns( $columns ) {
    $columns['col_2'] = '2';

    return $columns;
}
add_filter( 'manage_edit-genre_columns', 'add_genre_columns' );

/**
 * Add taxonomy custom column content the default way
 *
 * @param string $content = column content.
 * @param string $column_name = column name.
 * @param int    $term_id = term id.
 */
function add_genre_column_content( $content, $column_name, $term_id ) {
    $term = get_term( $term_id, 'genre' );

    if ( 'col_2' === $column_name ) {
        $content = $term_id;
    }

    echo $content;
}
add_filter( 'manage_genre_custom_column', 'add_genre_column_content', 10, 3 );
johnbillion commented 6 years ago

The term ID is passed as the first parameter to the column callback function. So this should do the trick:

'col_1' => array(
    'title'    => '1',
    'function' => function( $term_id ) {
        echo $term_id;
    },
),
oraimbaud commented 6 years ago

Oh, thanks 🙈