How disable Gravatar in WordPress site and replace them with default image. Code example

How disable Gravatar in WordPress site and replace them with default image. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Add code below to your function.php file

//Remove gravatar
define('DEFAULT_AVATAR_URL', get_template_directory_uri() . '/img/default-user.svg');
function disable_gravatars( $avatar ) {
    return preg_replace( "/http.*?gravatar\.com[^\']*/", DEFAULT_AVATAR_URL, $avatar );
}
add_filter( 'get_avatar', 'disable_gravatars' );

Gravatar is a service that allows users to create a profile image that can be used across different websites.

The code starts by defining a constant called “DEFAULT_AVATAR_URL” which stores the URL of the default user image. The URL is obtained by concatenating the template directory URI with the path to the default user image.

The next part of the code defines a function called “disable_gravatars” which takes an avatar parameter. This function uses a regular expression to search for any URLs that contain “gravatar.com” and replaces them with the “DEFAULT_AVATAR_URL” defined earlier. The modified avatar is then returned.

Finally, the code adds a filter to the “get_avatar” function using the “add_filter” function. This filter hooks into the “get_avatar” function and applies the “disable_gravatars” function to modify the avatar before it is displayed.

In summary, this code is used to replace the default Gravatar image with a custom default user image on a website.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation