I want to use random avatar in my wordpress website from some images. we have some images for example 20, this images has same size and same format. how can set one of this picture (random) for every user.
tnx
In order to achieve this, one can modify the behavior of the function get_avatar().
You can add a filter to the avatar_defaults option, and modify the “avatars array” in order to get a new “Static” default avatar option in Discussion Settings. You can see an example here. You can add the filter to pre_option_avatar_default , and return any URL you want. I will use the second option, so the URL returned will be picked up randomly from a set.
// Random Chameleon Avatar
add_filter( 'pre_option_avatar_default', 'chameleon_default_avatar' );
function chameleon_default_avatar ( $value )
{
return admin_url( 'images' ) . '/cham_avatars/cham_avatar'.rand( 0 , 13 ).'.jpg';
}
// Random Chameleon Avatar
add_filter( 'pre_option_avatar_default', 'chameleon_default_avatar' );
function chameleon_default_avatar ( $value )
{
return admin_url( 'images' ) . '/cham_avatars/cham_avatar'.rand( 0 , 13 ).'.jpg';
}
This will modify the regular flow WordPress uses, and return a custom random avatar. I created 14 avatars, named them cham_avatar0.jpg to cham_avatar13.jpg , and copied them into my wp_admin/images/cham_avatars folder .
The problem is that modifying the source code of WordPress directly is not ideal, because will be messed up in WP updates.
I recommend using the WordPress plugin Code Snippets for this. It helps keep your modifications organized and safe from updates, and you can easily enable or disable them.
Also, refer this link for more info https://ownyourbits.com/2017/01/31/custom-random-avatars-in-wordpress/