i have a user-meta kay named "avatar"; This value contains the URL of the image that the user upload. how i can to set this meta value as user avatar? i use this code and not work
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'avatar', true );
//check if it is a URL
if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
//return saved image
return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
}
//return normal
return $avatar;
}
i have a user-meta kay named "avatar"; This value contains the URL of the image that the user upload. how i can to set this meta value as user avatar? i use this code and not work
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'avatar', true );
//check if it is a URL
if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
//return saved image
return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
}
//return normal
return $avatar;
}
<?php
$id = get_current_user_id(); // this is for the current user, for other user just change this variable to the user id intended
$link = get_user_meta( $id, 'avatar', true ); ?>
<img src="<?php echo $link ?>" alt="">
That is your answer, but if you want to understand it just continue reading!.. (only after writing everything below I saw that your meta_key had the name "avatar", anyhow..)
This are 10 lines of my wp_usermeta table;
mysql> select * from wp_usermeta limit 5, 10;
+----------+---------+-----------------------+---------------------------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+-----------------------+---------------------------------+
| 6 | 1 | syntax_highlighting | true |
| 7 | 1 | comment_shortcuts | false |
| 8 | 1 | admin_color | light |
| 9 | 1 | use_ssl | 0 |
| 10 | 1 | show_admin_bar_front | false |
| 11 | 1 | locale | |
| 12 | 1 | wp_capabilities | a:1:{s:13:"administrator";b:1;} |
| 13 | 1 | wp_user_level | 10 |
| 14 | 1 | dismissed_wp_pointers | theme_editor_notice |
| 15 | 1 | show_welcome_panel | 0 |
+----------+---------+-----------------------+---------------------------------+
10 rows in set (0.00 sec)
If I want my admin_color (3rd line) I can run this code:
$link = get_user_meta( 1, 'admin_color', true );
echo '<p>'. $link . '</p>';
and this prints me "light", the meta_value, where your link path will be. The first argument is the user_id, the second the meta_key and the third is to say if you only want a single result or more, default is false, this means only one result, in this case true means as much as they are in the table.
This to say that you can use this function, but you need to know the meta_key value to get the meta_value, because the user id has a lot of meta_keys, and the meta_key is unique. to know the meta_key you can use this code, analize it and then change the user_id to your user_id and the meta_key to your meta key, and you will get your link path.
$id = get_current_user_id();
echo 'My id is '.$id;
$link = get_user_meta( $id, '', true );
echo '<pre>';
print_r($link);
now if it is there just use the first code with the proper arguments
<?php
$id = get_current_user_id();
$link = get_user_meta( $id, 'your_meta_key_target', true ); ?>
<img src="<?php echo $link ?>" alt="">
Issue in your code is sprintf
function and conditional
statements
In the sprintf function, corrected the placeholder for the image source to %s
instead of %
.
Added additional attributes to the image tag class, width, height
to match the standard output of wp avatars
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
// If is email, try and find user ID
if ( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ) {
$user = get_user_by( 'email', $id_or_email );
if ( $user ) {
$id_or_email = $user->ID;
}
}
// If not user ID, return
if ( ! is_numeric( $id_or_email ) ) {
return $avatar;
}
// Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'avatar', true );
// Check if it is a URL
if ( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
// Return saved image
return sprintf( '<img src="%s" alt="%s" class="avatar avatar-%d" width="%d" height="%d">', esc_url( $saved ), esc_attr( $alt ), $size, $size, $size );
}
// Return normal avatar
return $avatar;
}