钩子文档

wp_img_tag_add_decoding_attr

💡 云策文档标注

概述

wp_img_tag_add_decoding_attr 是一个 WordPress 过滤器,用于控制图像标签的 decoding 属性值。默认值为 async,开发者可以通过此过滤器自定义或移除该属性。

关键要点

  • 过滤器名称:wp_img_tag_add_decoding_attr
  • 作用:过滤并添加 decoding 属性到 img 标签,影响图像加载行为
  • 默认值:async,但可通过过滤器修改为 sync、auto 或移除(返回 falsey 值)
  • 参数:$value(属性值,可为字符串或 false/null)、$image(img 标签 HTML)、$context(调用上下文)
  • 引入版本:WordPress 6.1.0

代码示例

$filtered_decoding_attr = apply_filters(
    'wp_img_tag_add_decoding_attr',
    isset( $optimization_attrs['decoding'] ) ? $optimization_attrs['decoding'] : false,
    $image,
    $context
);

注意事项

  • 返回 falsey 值(如 false、null、空字符串)将省略 decoding 属性
  • 有效值包括 'async'、'sync' 或 'auto',需确保符合 HTML 规范

📄 原文内容

Filters the decoding attribute value to add to an image. Default async.

Description

Returning a falsey value will omit the attribute.

Parameters

$valuestring|false|null
The decoding attribute value. Returning a falsey value will result in the attribute being omitted for the image.
Otherwise, it may be: 'async', 'sync', or 'auto'. Defaults to false.
$imagestring
The HTML img tag to be filtered.
$contextstring
Additional context about how the function was called or where the img tag is.

Source

$filtered_decoding_attr = apply_filters(
	'wp_img_tag_add_decoding_attr',
	isset( $optimization_attrs['decoding'] ) ? $optimization_attrs['decoding'] : false,
	$image,
	$context
);

Changelog

Version Description
6.1.0 Introduced.