delarge
12/15/2017 - 1:11 PM

ACF Avatar

Wordpress ACF custom avatar

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
	'key' => 'group_550072d248855',
	'title' => 'Profile image',
	'fields' => array(
		array(
			'key' => 'field_550072d432bc7',
			'label' => 'Profile image',
			'name' => 'user__avatar',
			'type' => 'image',
			'instructions' => '',
			'required' => 0,
			'conditional_logic' => 0,
			'wrapper' => array(
				'width' => '',
				'class' => '',
				'id' => '',
			),
			'return_format' => 'array',
			'preview_size' => 'medium',
			'library' => 'all',
			'min_width' => '',
			'min_height' => '',
			'min_size' => '',
			'max_width' => '',
			'max_height' => '',
			'max_size' => '',
			'mime_types' => '',
		),
	),
	'location' => array(
		array(
			array(
				'param' => 'user_form',
				'operator' => '==',
				'value' => 'edit',
			),
		),
	),
	'menu_order' => 0,
	'position' => 'acf_after_title',
	'style' => 'default',
	'label_placement' => 'top',
	'instruction_placement' => 'label',
	'hide_on_screen' => '',
	'active' => 1,
	'description' => '',
));

endif;
<?php

// ACF avatar upload
/**
 * 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', 'beatm_acf_profile_avatar', 10, 5);
function beatm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
	// Get user by id or email
	$user       = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
	// Get the user id
	$user_id    = $user->ID;
	// Get the file id
	$image_id   = get_user_meta($user_id, 'user__avatar', true); // CHANGE TO YOUR FIELD NAME
	// Default image url ( this would be http://mywebsiteurl.com/wp-content/themes/theme-name/images/default_avatar.png )
	$avatar_url = get_stylesheet_directory_uri() . '/_/img/default_avatar.jpg'; // CHANGE TO YOUR DEFAULT AVATAR LOCATION

	// If we have a local avatar, get the url for it
	if ( $image_id ) {
		// Get the file url
		$image_url  = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
		$avatar_url = $image_url[0];
	}

	$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" />';
	return $avatar;
}

?>