PHP script for getting and customizing avatar images in WordPress

Source: Internet
Author: User
This article describes how to compile PHP scripts for obtaining and customizing avatar images in WordPress, including get_avatar () and alt tags. For more information, see Get_avatar () (get Avatar)
The get_avatar () function is used to obtain the code of the top mailbox or user's profile picture, which is frequently used in the comment list.

This function provides a get_avatar filter to filter the Html code (img tag) of the Avatar ).

If the "show avatar option" is disabled in the "discussion" of "settings" in the background, False is returned.

Usage

get_avatar( $id_or_email, $size, $default, $alt );

Parameters

$ Id_or_email

(Integer | string | object) (required) user ID, email address, comment, or user object. If you can use the get_the_author_meta ('id') function in the loop to call the author of the current article.

Default value: None

$ Size

(Integer) (optional) the size of the avatar. The maximum value is 512. the unit is pixel (px ).

Default value: 96
Get_avatar () (get Avatar)
$ Default

(String) (optional) if no picture is returned, the default value is "mysterious person", which can be the image URL.

Default value: Null string (mysterious person)

$ Alt

(String) (optional) alt attribute content of the img label.

Default value: False

Return value

(String | Boolean) returns the img label of an avatar. if the "show avatar option" is disabled in the "discussion" section of "settings" in the background, False is returned.

Example

Comment list:

<?php echo get_avatar( $GLOBALS['comment'] ); ?>

Loop to get the current article author's profile picture:

<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>

Custom email:

<? Php echo get_avatar ('email @ example.com ', 32, '', 'custom mailboxes');?>


Alt tags for custom avatar images
WordPress uses the Gravatar Avatar by default. it is called by the get_avatar () function and is generally used to call comments.

The fourth attribute of the get_avatar () function $ alt can be used to set the alt tag of the returned Code. However, most people do not set the tag when using the tag, which becomes blank.

Search engine crawlers cannot read the content of images. they only rely on their alt tags. images without alt tags are very bad for SEO.

If you want to add an alt tag to all portraits, you can put the following code in the topic's functions. php (learn more) file.

/*** Alt tags for WordPress custom avatar images * alt tags for different life cycles * http://www.endskin.com/avatar-alt/#/function Bing_avatar_alt ($ avatar) {$ alt = 'gravatar profile '; // content of the alt tag $ before = strpos ($ avatar, "alt = '"); $ after = strpos ($ avatar, "'", $ before ); if ($ before ==== false | $ after === false) return $ avatar; $ alt = esc_attr ($ alt); return substr ($ avatar, 0, $ before ). "alt = '$ alt ". substr ($ avatar, $ after + strlen ("'");} add_filter ('Get _ avatar', 'Bing _ avatar_alt ');

In this case, pay attention to modifying the $ alt variable in the code as the content of the alt tag to be modified.

Different alt tags are generated based on different life cycles.

If you want to place different alt tags based on different people, use the following example code:

/*** Alt tag of WordPress custom avatar image * http://www.endskin.com/avatar-alt/#/function Bing_avatar_alt ($ avatar, $ id_or_email) {if ($ id_or_email = 'bingoblog @ 163.com '| $ id_or_email = 1) {$ alt = 'master Gravatar profile '; // The alt tag content of the blogger} else {$ alt = 'gravatar avatar '; // The alt tag content of common visitors} $ before = strpos ($ avatar, "alt = '"); $ after = strpos ($ avatar, "'", $ before); if ($ before = false | $ after = false) return $ avatar; $ alt = esc_attr ($ alt); return substr ($ avatar, 0, $ before ). "alt = '$ alt ". substr ($ avatar, $ after + strlen ("'");} add_filter ('Get _ avatar', 'Bing _ avatar_alt ', 10, 2 );

If the alt label is already set, do not replace it.

If you need to use the following code when you do not replace the content of the alt tag ($ alt attribute of the get_avatar () function:

/*** Alt tag of WordPress custom avatar image * if the alt tag has been set, * paibing_avatar_alt ($ avatar, $ id_or_email, $ size, $ default, $ alt) is not replaced) {if (! Empty ($ alt) return $ avatar; $ alt = 'gravatar avatar '; // content of the alt tag $ before = strpos ($ avatar, "alt = '"); $ after = strpos ($ avatar, "'", $ before); if ($ before = false | $ after = false) return $ avatar; $ alt = esc_attr ($ alt); return substr ($ avatar, 0, $ before ). "alt = '$ alt ". substr ($ avatar, $ after + strlen ("'");} add_filter ('Get _ avatar', 'Bing _ avatar_alt ', 10, 5 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.