Custom Fields
Re-Envisioned
Support MB User Meta add and use local avatar with user meta extension

This topic contains 1 reply, has 2 voices, and was last updated by  Mauro 9 months, 1 week ago.

  • Creator
    Topic
  • #8308
    Resolved
    brkard
    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.

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #11288

    Mauro
    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 1 replies (of 1 total)

You must be logged in to reply to this topic.