• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Code Snippets » Custom avatar image based on image field in WP-Members

Custom avatar image based on image field in WP-Members

Chad Butler · May 22, 2018 ·

/**
 * Use a custom avatar from a WP-Members image field.
 * 
 * Note: you will need to edit the meta key for the field name
 * at the beginning of the code snippet below (default: "image_test").
 * The rest of the function does not need to be changed, but could
 * be depending on what you want your output to be.
 */
add_filter( 'get_avatar', function( $avatar, $id_or_email, $size, $default, $alt, $args ) {
    
    // What is the custom image field's meta key?
    // Set this value to match the meta key of your custom image field.
    $meta_key = "image_test";
    
    // Nothing really to change below here, unless
    // you want to change the <img> tag HTML.
    $user = false;
    if ( is_numeric( $id_or_email ) ) {
        $user = get_user_by( 'id' , (int)$id_or_email );
    } elseif ( is_object( $id_or_email ) ) {
        if ( ! empty( $id_or_email->user_id ) ) {
            $id = (int)$id_or_email->user_id;
            $user = get_user_by( 'id' , $id );
        }
    } else {
        $user = get_user_by( 'email', $id_or_email );    
    }
    if ( $user && is_object( $user ) ) {
        $post_id = get_user_meta( $user->ID, $meta_key, true );
        if ( $post_id ) {
            $attachment_url = wp_get_attachment_url( $post_id );
        
            // HTML for the avatar <img> tag.  This is WP default.
            // $avatar = '<img alt="' . $alt . '" src="' . $attachment_url . '" class="avatar avatar-' . $size . ' photo" height="' . $size . '" width="' . $size . '" />';
            $avatar = sprintf(
                "<img alt='%s' src='%s' class='%s' height='%d' width='%d' %s/>",
                esc_attr( $alt ),
                esc_url( $attachment_url ),
                esc_attr( "avatar avatar-" . $size . " photo" ),                
                (int) $args['height'],
                (int) $args['width'],
                $args['extra_attr']
            );
        }
    }
    return $avatar;
}, 10, 6 );

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • Using a code snippets plugin
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy