How to Get Your Gravatar Image URL / WordPress

Having a profile picture also known as an “avatar” is quite essential online. We upload our best profile pictures on websites and social sites to be legit, trustworthy and for people to better recognize our online presence.

WordPress has its own service for providing user profile images and it is called Gravatar. We can also incorporate this into our own custom themes. This post will walk you through a few approaches on how you can retrieve profile pictures from Gravatar.

Using Gravatar

Let’s start from the basics. WordPress has special built-in functionality, get_avatar, which allows us to get the gravatar image. This function requires two parameters: the user ID or email and the size of the image to display. Here is an example.

 $ user_id = get_the_ Author_meta ('ID'); tiếng vang get_avatar ($ user_id, 80); 

If you prefer to use user email, please fill in get_the_ Author_meta () function with Email người dùng:

 $ user_id = get_the_ Author_meta ('user_email'); tiếng vang get_avatar ($ user_id, 80); 

Both examples will give the same result: user avatar with size 80px. In my case I will see my image.

However, the problem I’ve had with this function is that it generates the whole image; full label. Check out the source code and you will find it as follows:

This makes things a bit difficult for us, for example, to insert additional classes or IDs into .

Also, we can only retrieve the image URL, instead of full element. Once we have received the URL, we can add it in with custom classes or added IDs.

Read More:   Top 10 Google Chrome RSS Feeds

How to get image URL

First, we’ll need to create a new PHP function in Functions.php of the WordPress theme you are using. Name the function like this:

 chức năng get_avatar_img_url  

Getting the Gravatar image requires the user’s email; make sure email is registered in Gravatar to see output. Call the author user email, like that.

 hàm get_avatar_img_url $ user_email = get_the_ Author_meta ('user_email');  

Gravatar image URL specified with http://gravatar.com/avatar/ and followed by hash md5 (encoded value) of the email address. To return email address into “md5 hash” value, we can use PHP built-in function, md5 (). Therefore, we set the Gravatar image URL this way:

 hàm get_avatar_img_url $ user_email = get_the_ Author_meta ('user_email'); $ url="http://gravatar.com/avatar/". md5 ($ user_email);  

Next, we need to include a few required parameters in the URL which are the default image size and fallback image if the image is not registered in Gravatar. To do so, we will use a WordPress function named add_query_arg.

 hàm get_avatar_img_url () $ user_email = get_the_ Author_meta ('user_email'); $ url="http://gravatar.com/avatar/". md5 ($ user_email); $ url = add_query_arg (mảng ('s' => 80, 'd' => 'mm',), $ url); trả về esc_url_raw ($ url);  

This add_query_arg The function will add the parameter at the end of the URL. In our case it will output ?s = 80 & d = mm which sets the image size to 80 pixels and sets the default avatar to mm (Riddle).

Now just use PHP tiếng vang to output the URL in element, like so:

 $ avatar_url = get_avatar_img_url (); tiếng vang ''; 

Last, Ched All sent you details about the topic “How to Get Your Gravatar Image URL / WordPress
❤️️”.Hope with useful information that the article “How to Get Your Gravatar Image URL / WordPress
” It will help readers to be more interested in “How to Get Your Gravatar Image URL / WordPress
[ ❤️️❤️️ ]”.

Read More:   Quick Tip How to Recover Lost WordPress (LocalHost) Password / WordPress

Posts “How to Get Your Gravatar Image URL / WordPress
” posted by on 2023-01-17 14:36:25. Thank you for reading the article at Chedall.com

Back to top button