Skip to content

Commit b104705

Browse files
committed
Add author name to term name on term creation
1 parent a2c3fa9 commit b104705

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

inc/namespace.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ function bootstrap() : void {
5858
add_filter( 'the_author', __NAMESPACE__ . '\\filter_the_author_for_rss' );
5959
add_filter( 'comment_moderation_recipients', __NAMESPACE__ . '\\filter_comment_moderation_recipients', 10, 2 );
6060
add_filter( 'comment_notification_recipients', __NAMESPACE__ . '\\filter_comment_notification_recipients', 10, 2 );
61+
add_filter( 'wp_insert_term_data', __NAMESPACE__ . '\\add_term_name', 10, 3 );
62+
}
63+
64+
/**
65+
* Set term name to user display name on term creation.
66+
*
67+
* @param array $data Term data.
68+
* @param string $taxonomy Taxonomy.
69+
* @param array $args wp_insert_term() args.
70+
* @return array
71+
*/
72+
function add_term_name( array $data, string $taxonomy, array $args ) : array {
73+
if ( $taxonomy !== TAXONOMY ) {
74+
return $data;
75+
}
76+
$user_id = (int) $data['slug'] ?? null;
77+
if ( ! $user_id ) {
78+
return $data;
79+
}
80+
81+
$user = get_userdata( $user_id );
82+
if ( ! $user ) {
83+
return $data;
84+
}
85+
86+
$data['name'] = $user->display_name;
87+
return $data;
6188
}
6289

6390
/**

0 commit comments

Comments
 (0)