How to add date and author column in custom taxonomy term page.

1) First we need to create a custom taxonomy name company. check below code and copy it to your functions.php file

/*
* Create Company
*/
add_action( 'init', 'ab_create_company_hierarchical_taxonomy', 0 );

function ab_create_company_hierarchical_taxonomy() {
   
  $labels = array(
    'name' => _x( 'Company', 'taxonomy general name' ),
    'singular_name' => _x( 'Company', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Company' ),
    'all_items' => __( 'All Company' ),
    'parent_item' => __( 'Parent Company' ),
    'parent_item_colon' => __( 'Parent Company:' ),
    'edit_item' => __( 'Edit Company' ), 
    'update_item' => __( 'Update Company' ),
    'add_new_item' => __( 'Add New Company' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Company' ),
  );    

  register_taxonomy('company',array('post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'company' ),
  ));
  
}

2) if your taxonomy is created copy below code and pest in to functions.php file

// REGISTER TERM META

add_action( 'init', '_ab_register_company_meta_field' );

function _ab_register_company_meta_field() {

    register_meta( 'term', '_company_meta_date', '_sanitize_company_meta_date' );
    register_meta( 'term', '_company_meta_author', '_sanitize_company_meta_author' );
}

// SANITIZE DATA

function _sanitize_company_meta_date ( $date ) {
    return sanitize_text_field ($date);
}
function _sanitize_company_meta_author ( $author ) {
    return sanitize_text_field ($author);
}

function _company_date( $term_id ) {
  $date = get_term_meta( $term_id, '_company_meta_date', true );
  $date = _sanitize_company_meta_date( $date );
  return $date;
}

function _company_author( $term_id ) {
  $author = get_term_meta( $term_id, '_company_meta_author', true );
  $author = _sanitize_company_meta_author( $author );
  return $author;
}

// ADD FIELD TO CATEGORY TERM PAGE
add_action( 'company_add_form_fields', '_ab_add_company_meta_form_field' );

function _ab_add_company_meta_form_field() { 
    $user = wp_get_current_user()
    ?>
    <?php wp_nonce_field( basename( __FILE__ ), 'company_meta_date_nonce' ); ?>
    <div class="form-field term-meta-date-wrap">
        <input type="hidden" name="company_meta_date" id="term-meta-date" value="<?php echo date("Y-m-d"); ?>" class="term-meta-date-field" readonly/>
        <input type="hidden" name="company_meta_author" id="term-meta-author" value="<?php echo $user->ID; ?>" class="term-meta-author-field" readonly/>
    </div>
<?php }


// ADD FIELD TO CATEGORY EDIT PAGE

add_action( 'company_edit_form_fields', '_ab_edit_company_meta_form_field' );

function _ab_edit_company_meta_form_field( $term ) {

    $company_date  = _company_date( $term->term_id );
    $company_author  = _company_author( $term->term_id );

    if ( ! $company_date )
        $company_date = ""; ?>
    <tr class="form-field term-meta-date-wrap">
        <th scope="row"><label for="term-meta-date"><?php _e( 'Date', 'ab' ); ?></label></th>
        <td>
            <?php wp_nonce_field( basename( __FILE__ ), 'company_meta_date_nonce' ); ?>
            <input type="text" name="company_meta_date" id="term-meta-date" value="<?php echo esc_attr( $company_date ); ?>" class="term-meta-date-field"  />
            <input type="text" name="company_meta_author" id="term-meta-author" value="<?php echo esc_attr( $company_author ); ?>" class="term-meta-author-field"  />
        </td>
    </tr>
<?php }


// SAVE TERM META (on term edit & create)

add_action( 'edited_company',   '_ab_save_company_meta_fields' );
add_action( 'create_company', '_ab_save_company_meta_fields' );

function _ab_save_company_meta_fields( $term_id ) {

    if ( ! isset( $_POST['company_meta_date_nonce'] ) || ! wp_verify_nonce( $_POST['company_meta_date_nonce'], basename( __FILE__ ) ) )
        return;

    $old_date  = _company_date( $term_id );
    $new_date = isset( $_POST['company_meta_date'] ) ? _sanitize_company_meta_date ( $_POST['company_meta_date'] ) : '';
    

    if ( $old_date && '' === $new_date)
        delete_term_meta( $term_id, '_company_meta_date' );

    else if ( $old_date !== $new_date )
        update_term_meta( $term_id, '_company_meta_date', $new_date );

    $old_author  = _company_author( $term_id );
    $new_author = isset( $_POST['company_meta_author'] ) ? _sanitize_company_meta_author ( $_POST['company_meta_author'] ) : '';

    if ( $old_author && '' === $new_author)   
        delete_term_meta( $term_id, '_company_meta_author' );
    else if ( $old_author !== $new_author)
        update_term_meta( $term_id, '_company_meta_author', $new_author );
}

// MODIFY COLUMNS (add our meta to the list)

add_filter( 'manage_edit-company_columns', '_ab_edit_term_columns' );

function _ab_edit_term_columns( $columns ) {

    $columns['_company_meta_date'] = __( 'Date', 'ab' );
    $columns['_company_meta_author'] = __( 'Author', 'ab' );

    return $columns;
}


// RENDER COLUMNS

add_filter( 'manage_company_custom_column', '_ab_manage_term_custom_column', 10, 3 );

function _ab_manage_term_custom_column( $out, $column, $term_id ) {

    if ( '_company_meta_date' === $column ) {

        $date  = _company_date( $term_id );

        if ( ! $date )
            $date = '';

        $out = sprintf( '<span class="term-meta-date-block" style="" >%s</div>', esc_attr( $date ) );
    }
    if ( '_company_meta_author' === $column ) {

        $author  = _company_author( $term_id );

        if ( ! $author )
            $author = '';

        $out = sprintf( '<span class="term-meta-author-block" style="" >%s</div>', esc_attr( $author ) );
    }

    return $out;
}

// ADD JAVASCRIPT & STYLES TO COLUMNS

add_action( 'admin_enqueue_scripts', '_ab_admin_enqueue_scripts' );

function _ab_admin_enqueue_scripts( $hook_suffix ) {

    if ( 'edit-tags.php' !== $hook_suffix || 'company' !== get_current_screen()->taxonomy )
        return;

    add_action( 'admin_footer', '_ab_meta_term_text_print_scripts' );
}

function _ab_meta_term_text_print_scripts() { ?>

    <script type="text/javascript">
        jQuery( document ).ready( function( $ ) {
             $input_field = $( '.term-meta-date-field' );
        } );
    </script>
<?php }

Leave a Reply

Your email address will not be published. Required fields are marked *