wp_img_tag_add_loading_attr()WP 5.5.0

Устарела с версии 6.3.0. Больше не поддерживается и может быть удалена. Используйте wp_img_tag_add_loading_optimization_attrs().

Adds loading attribute to an img HTML tag.

Хуки из функции

Возвращает

Строку. Converted img tag with loading attribute added.

Использование

wp_img_tag_add_loading_attr( $image, $context );
$image(строка) (обязательный)
The HTML img tag where the attribute should be added.
$context(строка) (обязательный)
Additional context to pass to the filters.

Заметки

Список изменений

С версии 5.5.0 Введена.
Устарела с 6.3.0 Use wp_img_tag_add_loading_optimization_attrs() instead.

Код wp_img_tag_add_loading_attr() WP 6.5.2

function wp_img_tag_add_loading_attr( $image, $context ) {
	_deprecated_function( __FUNCTION__, '6.3.0', 'wp_img_tag_add_loading_optimization_attrs()' );
	/*
	 * Get loading attribute value to use. This must occur before the conditional check below so that even images that
	 * are ineligible for being lazy-loaded are considered.
	 */
	$value = wp_get_loading_attr_default( $context );

	// Images should have source and dimension attributes for the `loading` attribute to be added.
	if ( ! str_contains( $image, ' src="' ) || ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) {
		return $image;
	}

	/** This filter is documented in wp-admin/includes/media.php */
	$value = apply_filters( 'wp_img_tag_add_loading_attr', $value, $image, $context );

	if ( $value ) {
		if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
			$value = 'lazy';
		}

		return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
	}

	return $image;
}