WordPress _php tips for getting and customizing your avatar images in PHP scripts

Source: Internet
Author: User

Get_avatar () (Get Avatar)
the Get_avatar () function is used to get the top mailbox or the user's avatar code, which is very common in the comment list.

This function provides a Get_avatar filter to filter the Avatar's Html code (the IMG tag).

Returns False if display avatar options is turned off in discussion in background settings.

Usage

Get_avatar ($id _or_email, $size, $default, $alt);

Parameters

$id _or_email

(Integer | string | Object) (must) the user's ID, mailbox, comment, or the user's object. If you can use the Get_the_author_meta (' ID ') function in a loop to invoke the author of the current article.

Default value: None

$size

(optional) The size of the avatar, with a maximum of 512, in pixels (px).

Default value: 96
Get_avatar () (Get Avatar)
$default

(optional) If there is no picture returned by the Avatar, the default is "Mystic Person", which can be a picture URL.

Default value: Empty string (mysterious person)

$alt

(optional) The ALT attribute content of the avatar img tag.

Default value: False

return value

(String | Boolean) returns an IMG tag for an avatar that returns False if the display Avatar option is turned off in discussion in background settings.

Example

Comments List:

<?php Echo Get_avatar ($GLOBALS [' comment ']);?>

Loop, get the current article author Avatar:

<?php Echo Get_avatar (Get_the_author_meta (' ID ');?>

Custom mailboxes:

<?php echo Get_avatar (' email@example.com ', 32, ', ' Custom mailbox ');?>


Customize the Alt label for the Avatar picture
WordPress defaults to use the Gravatar avatar, through the Get_avatar () function call, commonly used when calling comments.

Using the fourth attribute of the Get_avatar () function $alt can set the ALT tag for the return code, but most people do not set it when they use it, and it becomes blank.

Search Engine crawler is unable to read what the picture is exactly what content, can only rely on its alt tag, no alt label picture, SEO is very bad.

If you want to unify all the avatars with an ALT tag, you can put the following code in the functions.php (read more) file of the topic.

/**
  *wordpress The alt tag of a custom avatar picture
  * According to different life into different alt tags
  *http://www.endskin.com/avatar-alt/
* * function Bing_avatar_alt ($avatar) {
  $alt = ' Gravatar avatar '; the contents of the//alt label
  $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 ');

So, notice that the $alt variable in the code is the ALT tag you want to modify.

Different ALT tags based on different life

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

/**
  *wordpress custom avatar Picture alt tag
  *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 = ' bo main gravatar Avatar '; /Bo main alt label Content
  }else{
    $alt = ' Gravatar avatar ';//alt label content for ordinary 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 you have already set the ALT tag, do not replace

If you need to set the ALT tag content (the Get_avatar () function's $alt property), you can use the following code when you do not replace it:

/**
  *wordpress The alt tag for a custom avatar picture
  * If you have already set the ALT tag, do not replace
  *http://www.endskin.com/avatar-alt/
. function Bing_avatar_alt ($avatar, $id _or_email, $size, $default, $alt) {
  if (!empty ($alt)) return $avatar;
  $alt = ' Gravatar avatar '; the contents of the//alt label
  $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.