Di come aggiungere foto profilo utente in WordPress, ne avevamo già parlato in un precedente articolo (vedi => Cambiare immagine utente su wordpress). In breve si consigliava l’utilizzo di Gravatar.com
Considerando che molte persone non vorrebbero essere costrette ad usare il metodo Gravatar, è possibile creare un Avatar personalizzato impostando delle funzioni in WordPress.
Come sempre il consiglio è di agire su un tema child ed aggiungere le seguenti funzioni create da Bill Erickson e che riporto fedelmente poichè funzionano perfettamente per il nostro scopo:
/* Add Custom Avatar Field */
function be_custom_avatar_field( $user ) { ?>
<h3>Custom Avatar</h3>
<table>
<tr>
<th><label for="be_custom_avatar">Custom Avatar URL:</label></th>
<td>
<input type="text" name="be_custom_avatar" id="be_custom_avatar" value="<?php echo esc_attr( get_the_author_meta( 'be_custom_avatar', $user->ID ) ); ?>" /><br />
<span>Type in the URL of the image you'd like to use as your avatar. This will override your default Gravatar, or show up if you don't have a Gravatar. <br /><strong>Image should be 70x70 pixels.</strong></span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'be_custom_avatar_field' );
add_action( 'edit_user_profile', 'be_custom_avatar_field' );
/* Save Custom Avatar Field*/
function be_save_custom_avatar_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_usermeta( $user_id, 'be_custom_avatar', $_POST['be_custom_avatar'] );
}
add_action( 'personal_options_update', 'be_save_custom_avatar_field' );
add_action( 'edit_user_profile_update', 'be_save_custom_avatar_field' );
/* Use Custom Avatar if Provided */
function be_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) {
// If provided an email and it doesn't exist as WP user, return avatar since there can't be a custom avatar
$email = is_object( $id_or_email ) ? $id_or_email->comment_author_email : $id_or_email;
if( is_email( $email ) && ! email_exists( $email ) )
return $avatar;
$custom_avatar = get_the_author_meta('be_custom_avatar');
if ($custom_avatar)
$return = '<img src="'.$custom_avatar.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
elseif ($avatar)
$return = $avatar;
else
$return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
return $return;
}
add_filter('get_avatar', 'be_gravatar_filter', 10, 5);
Fatto ciò puoi da subito inserire la tua immagine preferita associata al tuo profilo in “Utenti -> Il tuo profilo” e che sarà visibile sotto ogni articolo del tuo blog.
Per qualsiasi domanda, lascia un tuo commento.
Articoli correlati
Su come creare un ruolo utente personali...
Prima di imparare ad utilizzare uno dei ...
Molti temi WordPress mostrano automatica...
La visualizzazione di articoli random pe...