Assign specific default Profile Photo to a User Role

Hello Ultimate Member Users & Developers! In this tutorial you will learn how to add multiple default profile photo for different roles.
Let’s look at the code snippets below before I explain each code line:
add_filter("um_user_avatar_url_filter",function( $url, $user_id, $data ){ um_fetch_user( $user_id ); $role = um_user("role"); if( $role == 'subscriber' ){ return "https://via.placeholder.com/150/0000FF/FFFFFF?text=Subscriber"; }else if( $role == 'administrator' ){ return "https://via.placeholder.com/150/FF0000/FFFFFF?text=Administrator"; }else if( $role == 'bbp_spectator' ){ return "https://via.placeholder.com/150/FFFF00/FFFFFF?text=Spectator"; } return $url; // default URL }, 999999, 3);
Ultimate Member has a setting for adding a default profile photo in WP Admin > Ultimate Member > Settings > Appearance > see Default Profile Photo. As you can see it, we are only allowed to add a Default Profile for global usage – there’s no additional options to add and assign a default profile photo to specific user roles.
Let’s see how the code works.
add_filter("um_user_avatar_url_filter",function( $url, $user_id, $data ){ // do something return $url; });
Above, we are using a filter hook to retrieve the default Profile Photo and modify it with our own source of Profile Photo. There are 3 filter hook parameters:
$url – this passes the default Profile Photo URL added via the settings page.
$user_id – a reference for the current user in the member directory loop or profile form.
$data – Profile Photo data which we won’t use in this tutorial but you might use it for your own purpose.
um_fetch_user( $user_id ); $role = um_user("role"); if( $role == 'subscriber' ){ return "https://randomuser.me/api/portraits/men/80.jpg"; }else if( $role == 'administrator' ){ return "https://randomuser.me/api/portraits/men/50.jpg"; }else if( $role == 'editor' ){ return "https://randomuser.me/api/portraits/men/50.jpg"; } return $url; // default URL
To retrieve the current’s user role in the Loop or Single Profile page, you can use the um_fetch_user
function to retrieve the user’s data. These data includes the role
value.
$role = um_user("role"); if( $role == 'subscriber' ){ return "https://randomuser.me/api/portraits/men/80.jpg"; }
The above shows that when a user has a subscriber
role, it swaps the set default profile with the custom Profile Photo URL.
Ensure that the role slug is correct. You can check the Role slugs in WP Admin > Ultimaet Member > User Roles.
Now, you can use the first code snippets above and add it to your theme’s function file or via Code Snippets plugin.
I hope that you find this helpful. Please feel free to leave a comment, feedback and some goodvibes. Don’t forget to subscribe to my newsletter!
Charlotte
Hey Champ, I just wanted to say how brilliantly helpful this code is. It is exactly what I have been trying and failing to do for my ultimate member site for about 3 weeks (I am not a techie), I really appreciate it. This helps me anonymity future site members, as well as provide visual standardisation for the profiles.