get_gravatar (function)

Last updated: Mon, Apr 17, 2023 10:26 am
Return to Knowledgebase

Purpose

Used to generate a Gravatar URL for a given email address.

Location

users/helpers/us_helpers.php

Parameters

# Parameter Data Type Required Description
1 $email string Yes the email address to generate the Gravatar URL for
2 $s = 120 integer No the size of the Gravatar image in pixels (default: 120)
3 $d = 'mm' string No the default image to use if the email address does not have a Gravatar associated with it (default: 'mm' for a mystery man image)
4 $r = 'pg' string No the rating of the Gravatar image ('g', 'pg', 'r', or 'x') to determine whether explicit content is allowed (default: 'pg')
5 $img = false bool No whether to wrap the Gravatar URL in an <img> tag (default: false)
6 $atts = [] array No an array of additional attributes to include in the <img> tag, such as class or alt
7 $d = 'mm' Yes undocumented
8 $r = 'pg' Yes undocumented

Returns

Data Type Description of Returned Data
string If $img is false, the function returns a string containing the URL for the Gravatar image. If $img is true, the function returns an HTML string containing an tag with the Gravatar image URL and any additional attributes specified in $atts.

Further Documentation:

Example usage:

$email = 'example@example.com';
$gravatar_url = get_gravatar($email, 200, 'identicon', 'g', false);
echo $gravatar_url; // output: https://www.gravatar.com/avatar/2d30c67...8?s=200&d=identicon&r=g

$gravatar_html = get_gravatar($email, 200, 'identicon', 'g', true, ['class' => 'avatar']);
echo $gravatar_html; // output: