Support MB User Meta add and use local avatar with user meta extension Reply To: add and use local avatar with user meta extension

#11288
MauroMauro
Participant

This is actually quite easy and will add the profile picture in your wp-admin profile screen.


// Add User Profile Support
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_user_meta_boxes' ));
function your_prefix_register_user_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
            'title' => 'Profile Picture',
            'id' => 'profile_picture',
            'fields' => array(
                
                array (
                    'id' => 'custom_avatar',
                    'type' => 'single_image',
                    'name' => 'Upload Avatar',
                ),
            ),
            'type' => 'user',
        );
        return $meta_boxes;
    }

To get the avatar URL use:



// Check if MetaBox is available
if (function_exists('rwmb_meta')) {
  // Get the attachment object
  $avatar = rwmb_meta( 'custom_avatar', array( 'object_type' => 'user' ), $user_id );

// Check if the user has a custom avatar, get the URL
  if ($avatar) $avatar_url = $avatar['url']; // this will get the 150x150 sized avatar

// If no avatar is set, use your own custom one
  else $avatar_url = site_url().'path/to/your/default/avatar.jpg';
}