/** * 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.