add and use local avatar with user meta extension

Support MB User Meta add and use local avatar with user meta extensionResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8308
    brkardbrkard
    Participant

    Hi.

    I have tested user meta with example code here: https://docs.metabox.io/extensions/mb-user-meta/

    I cant add avatar to user profiles wiht that code.

    Is it possible to add and use local avatar with user meta extension by some way ?

    If so can you share the snippet code if possible ?

    Thanks.

    #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';
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.