ControlledChaos
1/18/2015 - 2:43 PM

Use a local avatar with Advanced Custom Fields image field.

Use a local avatar with Advanced Custom Fields image field.

<?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 );

?>

ACF Local Avatar

WordPress Snippet