Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use a local avatar with Advanced Custom Fields image field.

ACF Local Avatar

WordPress Snippet

<?php
function ccd_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = is_numeric( $id_or_email ) ? get_user_by( 'id', $id_or_email ) : get_user_by( 'email', $id_or_email );
$user_id = $user->ID;
$image_id = get_user_meta( $user_id, 'tsm_local_avatar', true );
$avatar_url = get_stylesheet_directory_uri() . '/images/default_avatar.png';
if ( $image_id ) {
$image_url = wp_get_attachment_image_src( $image_id, 'thumbnail' );
$avatar_url = $image_url[0];
}
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
return $avatar;
}
add_filter( 'get_avatar', 'ccd_acf_profile_avatar', 10, 5 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.