get_image_send_to_editor()
云策文档标注
概述
get_image_send_to_editor() 函数用于生成插入到编辑器中的图像 HTML 代码。它基于图像附件 ID 和其他参数构建 HTML,并支持通过过滤器进行自定义。
关键要点
- 函数接受多个参数,包括图像附件 ID、标题、对齐方式、URL、rel 属性、尺寸和 alt 文本。
- 内部调用 get_image_tag() 生成图像标签,并根据参数添加 rel 属性和链接包装。
- 提供 image_send_to_editor 过滤器,允许开发者修改输出的 HTML 代码。
- 返回字符串类型的 HTML,可直接插入到编辑器中。
代码示例
function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
$html = get_image_tag( $id, $alt, '', $align, $size );
if ( $rel ) {
if ( is_string( $rel ) ) {
$rel = ' rel="' . esc_attr( $rel ) . '"';
} else {
$rel = ' rel="attachment wp-att-' . (int) $id . '"';
}
} else {
$rel = '';
}
if ( $url ) {
$html = '' . $html . '';
}
$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt, $rel );
return $html;
}注意事项
- 参数 $rel 可以是布尔值或字符串,用于控制 rel 属性的生成。
- 函数自 WordPress 2.5.0 引入,并在 5.6.0 版本添加了 $rel 参数。
- 相关函数包括 get_image_tag()、esc_attr()、esc_url() 和 apply_filters()。
原文内容
Retrieves the image HTML to send to the editor.
Parameters
$idintrequired-
Image attachment ID.
$captionstringrequired-
Image caption.
$titlestringrequired-
Image title attribute.
$alignstringrequired-
Image CSS alignment property.
$urlstringoptional-
Image src URL. Default empty.
$relbool|stringoptional-
Value for rel attribute or whether to add a default value.
Default:
false $sizestring|int[]optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'medium'. $altstringoptional-
Image alt attribute. Default empty.
Source
function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
$html = get_image_tag( $id, $alt, '', $align, $size );
if ( $rel ) {
if ( is_string( $rel ) ) {
$rel = ' rel="' . esc_attr( $rel ) . '"';
} else {
$rel = ' rel="attachment wp-att-' . (int) $id . '"';
}
} else {
$rel = '';
}
if ( $url ) {
$html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
}
/**
* Filters the image HTML markup to send to the editor when inserting an image.
*
* @since 2.5.0
* @since 5.6.0 The `$rel` parameter was added.
*
* @param string $html The image HTML markup to send.
* @param int $id The attachment ID.
* @param string $caption The image caption.
* @param string $title The image title.
* @param string $align The image alignment.
* @param string $url The image source URL.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param string $alt The image alternative, or alt, text.
* @param string $rel The image rel attribute.
*/
$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt, $rel );
return $html;
}
Hooks
- apply_filters( ‘image_send_to_editor’, string $html, int $id, string $caption, string $title, string $align, string $url, string|int[] $size, string $alt, string $rel )
-
Filters the image HTML markup to send to the editor when inserting an image.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |