Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Forked from JiveDig/acf_image_local_avatar.php
Last active February 7, 2023 14:16
Show Gist options
  • Save ControlledChaos/cd6558219cfbc0a75d6f to your computer and use it in GitHub Desktop.
Save ControlledChaos/cd6558219cfbc0a75d6f to your computer and use it in GitHub Desktop.
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