image_send_to_editor

最后更新于:2021-11-27 00:59:45

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.

参数

$html

(string)
The image HTML markup to send.

$id

(int)
The attachment ID.

$caption

(string)
The image caption.

$title

(string)
The image title.

$align

(string)
The image alignment.

$url

(string)
The image source URL.

$size

(string|int[])
Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).

$alt

(string)
The image alternative, or alt, text.

$rel

(string)
The image rel attribute.

源文件

文件: gc-admin/includes/media.php

View on Trac

/**   
 * Add the data-media-description and data-media-full attributes to inserted image tag. 
 */
add_filter( 'image_send_to_editor', 'add_custom_data_attribute_send_to_editor', 10, 8 );
function add_custom_data_attribute_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ){  
	if( $id > 0 ){
		$post = get_post( $id );
		$media_data = array(
			$post->ID, // media id[0]
			$post->post_content, // media description
			$post->post_excerpt // media caption
		);			
		$img_size = gc_get_attachment_image_src($id, 'full'); // get media full size url
		$data  = sprintf( ' data-media-description="%s"', esc_attr( $media_data[1] ) ); // set data-media-description
		$data .= sprintf( ' data-media-url="%s" ', esc_url( $img_size[0] ) ); // set data-media-url
		$html = str_replace( "<img src", "<img{$data}src", $html ); // replace and add custom attributes
	}
	return $html;
}