Last active
September 2, 2021 09:07
-
-
Save JiveDig/25afb32c286aa78dad3a to your computer and use it in GitHub Desktop.
Use ACF image field on user profile for WP local avatars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Use ACF image field as avatar | |
* @author Mike Hemberger | |
* @link http://thestizmedia.com/acf-pro-simple-local-avatars/ | |
* @uses ACF Pro image field (tested return value set as Array ) | |
*/ | |
add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5); | |
function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) { | |
$user = ''; | |
// Get user by id or email | |
if ( is_numeric( $id_or_email ) ) { | |
$id = (int) $id_or_email; | |
$user = get_user_by( 'id' , $id ); | |
} 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 ) { | |
return $avatar; | |
} | |
// Get the user id | |
$user_id = $user->ID; | |
// Get the file id | |
$image_id = get_user_meta($user_id, 'tsm_local_avatar', true); // CHANGE TO YOUR FIELD NAME | |
// Bail if we don't have a local avatar | |
if ( ! $image_id ) { | |
return $avatar; | |
} | |
// Get the file size | |
$image_url = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name | |
// Get the file url | |
$avatar_url = $image_url[0]; | |
// Get the img markup | |
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>'; | |
// Return our new avatar | |
return $avatar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Curious how I would tweak this to replace Gravatar with a remote URL (for every user, stored in field "photo_url" in wp_usermeta), rather than a locally-stored file.
Changing 'cfm_avatar' to 'photo_url' per your comment doesn't work.
I'm eyeing up wp_get_attachment_image_src and thinking that isn't right for me...
Thanks.