Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created December 23, 2016 00:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tripflex/3190b9b3b484f99c0a4ba4f6c59cb44f to your computer and use it in GitHub Desktop.
Save tripflex/3190b9b3b484f99c0a4ba4f6c59cb44f to your computer and use it in GitHub Desktop.
How to set/use image URL from user's meta value as WordPress Avatar throughout entire site
<?php
add_filter( 'pre_get_avatar_data', 'smyles_set_avatar_based_on_user_meta', 10, 2 );
/**
* Use URL from User Meta for Avatar
*
* This will return a URL value set in the user's meta to use as an avatar, if it
* exists, and is a valid URL. Should work everywhere on the site unless you're
* using a custom avatar plugin that overrides or does not call core get_avatar_data()
*
* This was created to use with my WP Job Manager Field Editor plugin that saves a URL
* to the user's meta from an upload field, but should work with any site that sets a
* single URL value in the user's meta.
*
* WP Job Manager Field Editor:
* https://smyl.es/jmfe
*
* @requires WordPress 4.2+
*
* @author Myles McNamara
* @url https://smyl.es
* @updated December 22, 2016
*
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param mixed $id_or_email User ID, User Email, or WP_User/WP_Post/WP_Comment object
*
* @return array Non-null value in `url` key to short circuit get_avatar_data(), or null to continue
*/
function smyles_set_avatar_based_on_user_meta( $args, $id_or_email ){
// Set this to the full meta key set in Save As under Auto Populate tab (for WP Job Manager Field Editor)
$user_avatar_meta_key = '_user_avatar';
// Check for comment_ID
if( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ){
$id_or_email = get_comment( $id_or_email );
}
// Check if WP_Post
if( $id_or_email instanceof WP_Post ){
$user_id = $id_or_email->post_author;
}
// Check if WP_Comment
if( $id_or_email instanceof WP_Comment ){
if( ! empty( $id_or_email->user_id ) ){
$user_id = $id_or_email->user_id;
} elseif( ! empty( $id_or_email->comment_author_email ) ){
// If user_id not available, set as email address to handle below
$id_or_email = $id_or_email->comment_author_email;
}
}
if( is_numeric( $id_or_email ) ){
$user_id = $id_or_email;
} elseif( is_string( $id_or_email ) && strpos( $id_or_email, '@' ) ){
$id_or_email = get_user_by( 'email', $id_or_email );
}
// Last check, convert user object to ID
if( $id_or_email instanceof WP_User ){
$user_id = $id_or_email->ID;
}
// Now that we have a user ID, check meta for avatar file
if( ! empty( $user_id ) && is_numeric( $user_id ) ){
$meta_val = maybe_unserialize( get_user_meta( $user_id, $user_avatar_meta_key, TRUE ) );
if( ! empty( $meta_val ) ){
// Set to first value if returned value is array
if( is_array( $meta_val ) && ! empty( $meta_val[0] ) ){
$meta_val = $meta_val[0];
}
// As long as it's a valid URL, let's go ahead and set it
if( filter_var( $meta_val, FILTER_VALIDATE_URL ) ){
$args['url'] = $meta_val;
}
}
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment