Changing the way owners are displayed and searched

Owners (users, groups, projects, roles, etc.) are displayed in many places in the plugin and can be searched when publishing private content.

By default, the plugin is using sensible values to compute the way they are shown to you.

For example, a user is represented by default by its `display_name` property, groups are using the `title` property, etc.

If you want to change that, you can use a filter, one by owner type, to compute the text shown. Here is a first example


add_filter('cuar/core/ownership/owner-display-name?owner-type=usr', 'cuar_custom_user_display_value');
function cuar_custom_user_display_value($owner_id){
    // Get user from database
    $user = new WP_User($owner_id);

    // Show firstname lastname when set
    if(!empty($user->first_name) && !empty($user->last_name) {
        return $user->first_name . ' ' . $user->last_name;
    }
             
    // Else use whatever value computed by WordPress (email, nickname, etc.)
    return $user->display_name;
}

Here is another example for groups, where we want to add the group ID after the title:


add_filter('cuar/core/ownership/owner-display-name?owner-type=grp', 'cuar_custom_group_display_value');
function cuar_custom_group_display_value($owner_id){
    // Get group from database
    $post = new WP_Post($owner_id);

    return "({$post->ID}) {$post->title}";
}