钩子文档

get_image_tag

💡 云策文档标注

概述

get_image_tag 是一个 WordPress 过滤器,用于修改图像标签的 HTML 内容。它允许开发者在生成图像标签时自定义输出,例如调整属性或样式。

关键要点

  • 过滤器名称:get_image_tag
  • 参数包括:$html(HTML 内容)、$id(附件 ID)、$alt(alt 属性描述)、$title(title 属性描述)、$align(对齐类名部分)、$size(图像尺寸)
  • 可用于自定义图像标签的生成,如添加类、修改属性或调整尺寸处理

代码示例

add_filter( 'get_image_tag', 'custom_image_tag', 10, 6 );
function custom_image_tag( $html, $id, $alt, $title, $align, $size ) {
    // 自定义逻辑,例如添加额外类名
    $html = str_replace( 'class="', 'class="my-custom-class ', $html );
    return $html;
}

注意事项

  • 此过滤器在 WordPress 2.6.0 版本中引入
  • 参数 $size 可以是注册的图像尺寸名称或包含宽度和高度的数组
  • 相关函数 get_image_tag() 用于获取图像附件标签,位于 wp-includes/media.php

📄 原文内容

Filters the HTML content for the image tag.

Parameters

$htmlstring
HTML content for the image.
$idint
Attachment ID.
$altstring
Image description for the alt attribute.
$titlestring
Image description for the title attribute.
$alignstring
Part of the class name for aligning the image.
$sizestring|int[]
Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).

Source

return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );

Changelog

Version Description
2.6.0 Introduced.