Skip to content

Instantly share code, notes, and snippets.

@reatlat
Last active April 10, 2024 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reatlat/a84798425b45ad4f18f2b5be9e13a3ff to your computer and use it in GitHub Desktop.
Save reatlat/a84798425b45ad4f18f2b5be9e13a3ff to your computer and use it in GitHub Desktop.
This snippet allows users to upload a custom avatar image.
<?php
/**
* Custom Gravatar
*
* This snippet adds a custom gravatar field to the user profile page.
* It also adds a filter to replace the default gravatar with the custom one.
* To make it work, you need to have Advanced Custom Fields plugin installed and activated.
*
* Usage:
* 1. Copy this snippet to your theme's directory (e.g. /wp-content/themes/your-theme/inc/custom-gravatar.php)
* 2. Include this snippet in your functions.php file with the following code:
* require_once get_template_directory() . '/inc/custom-gravatar.php';
* 3. Add the following code to your theme's template file where you want to display the gravatar:
* echo get_avatar(get_the_author_meta('ID'), 96);
* 4. Go to the user profile page in the admin panel and upload a custom gravatar image.
* 5. Refresh the page where you added the code from step 3.
* 6. The default gravatar should be replaced with the custom one.
*
* @author Alex Zappa <alex@zappa.dev>
* @copyright 2024 Alex Zappa
*/
if (!defined('ABSPATH'))
exit;
if (class_exists('ACF')) {
add_action('acf/include_fields', function () {
if (!function_exists('acf_add_local_field_group')) {
return;
}
acf_add_local_field_group(array(
'key' => 'user_extra_options',
'title' => 'User Extra Options',
'fields' => array(
array(
'key' => 'user_extra_options__custom_gravatar',
'label' => 'Custom Gravatar',
'name' => 'custom_gravatar',
'aria-label' => '',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'id',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'preview_size' => 'thumbnail',
),
),
'location' => array(
array(
array(
'param' => 'user_form',
'operator' => '==',
'value' => 'all',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
});
function sunnypixels_custom_avatar($avatar, $id_or_email, $size, $default, $alt, $args)
{
$id = sunnypixels_get_user_id($id_or_email);
if (!$id)
return $avatar;
$custom_avatar = get_user_meta($id, 'custom_gravatar', true);
if ($custom_avatar) {
$custom_avatar = wp_get_attachment_image_src($custom_avatar)[0];
$avatar = "<img alt='{$alt}' src='{$custom_avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' {$args['extra_attr']} />";
//php_pretty_print($avatar);
}
return $avatar;
}
function sunnypixels_custom_avatar_url($url, $id_or_email, $args)
{
$id = sunnypixels_get_user_id($id_or_email);
if (!$id)
return $url;
$custom_avatar = get_user_meta($id, 'custom_gravatar', true);
if ($custom_avatar) {
$custom_avatar = wp_get_attachment_image_src($custom_avatar)[0];
$url = $custom_avatar;
}
return $url;
}
function sunnypixels_get_user_id($id_or_email)
{
if (is_numeric($id_or_email))
return $id_or_email;
if (is_object($id_or_email))
return $id_or_email->user_id;
if (is_string($id_or_email))
return get_user_by('email', $id_or_email) ? get_user_by('email', $id_or_email)->ID : 0;
return 0;
}
add_filter('get_avatar', 'sunnypixels_custom_avatar', 100500, 6);
add_filter('get_avatar_url', 'sunnypixels_custom_avatar_url', 100500, 3);
} else {
add_action('admin_notices', function () {
echo '<div class="notice notice-warning is-dismissible"><p>Advanced Custom Fields plugin is required for Custom Gravatar functionality.</p></div>';
});
}
@reatlat
Copy link
Author

reatlat commented Apr 10, 2024

Check out the new variation of the plugin too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment