Notifications
Clear all

[Solved] wpdumc_user_search_result

4 Posts
2 Users
0 Reactions
66 Views
Posts: 2
Customer
Topic starter
(@michael-gyura)
Member
Joined: 7 months ago

I am trying to use the filter wpdumc_user_search_result to only show a list of users with the Role of "administrator"

I can get the list to display only these users, but it breaks the select method as it pastes in a null value.  It also doesn't filter as I type, it just shows the full list. 

So, clearly I've done this wrong. 

I've not found any docs or notes in the code on what this output should look like and am hitting a wall. 

I am happy to pay for extra support or time with one of your devs to accomplish this.  

How do I filter the list of comment notifications to only show users with the role of "administrator?"

3 Replies
Astghik
Posts: 6440
Admin
(@astgh)
Illustrious Member
Joined: 8 years ago

We'll check and get back to you soon. 

Reply
Posts: 2
Customer
Topic starter
(@michael-gyura)
Member
Joined: 7 months ago

Thank you.  We were able to implement a solution.  For others, here is what we did. 

We have a custom user role of "Diocesan Admin," and we're now filtering to only show users with this role.  The code can be used and updated to meet your needs.  

 

/**
* Filters the list of mentionable users to only include users with the 'diocesan_admin' role.
*
* This function hooks into the `wpdumc_user_search_result` filter provided by the
* wpDiscuz User & Comment Mentioning add-on. It restricts the user search results
* so that only users with the custom role 'diocesan_admin' are returned based on the search term.
*
* @param array $users The original list of users returned by wpDiscuz.
* @param int $post_id The ID of the current post where the mention is used.
* @param string $search_term The term entered after the '@' symbol to search users.
*
* @return array $filtered_users Filtered list of users who have the 'diocesan_admin' role.
*/

add_filter('wpdumc_user_search_result', 'filter_diocesan_admin_mentions', 10, 3);

function filter_diocesan_admin_mentions($users, $post_id, $search_term) {
$filtered_users = [];

$args = [
'role' => 'diocesan_admin',
'search' => '*' . esc_attr($search_term) . '*',
'search_columns' => ['user_login', 'display_name'],
'fields' => ['ID', 'display_name', 'user_login', 'user_nicename']
];

$diocesan_users = get_users($args);

foreach ($diocesan_users as $user) {
$filtered_users[] = (object)[
'ID' => (string)$user->ID,
'display_name' => $user->display_name,
'value' => $user->user_nicename,
'user_nicename' => $user->user_nicename,
'avatar' => "<div class='ucm-avatar'>" . get_avatar($user->ID, 30) . "</div>"
];
}

return $filtered_users;
}
Reply
Astghik
Posts: 6440
Admin
(@astgh)
Illustrious Member
Joined: 8 years ago

This one can be used as well:

 
 add_filter('wpdumc_user_search_result', function ($users) {
     $filteredUsers = [];
     foreach ($users as $user) {
         $userId    = $user->ID;
         $userMeta  = get_userdata($userId);
         $userRoles = $userMeta->roles;
         if (in_array('administrator', $userRoles)) {
             $filteredUsers[] = $user;
         }
         return $filteredUsers;
     }
 });
Reply
Share:
Scroll to top