Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_img_tag_add_loading_attr ›

Seit5.5.0
Veraltetn/v
wp_img_tag_add_loading_attr ( $image, $context )
Parameter: (2)
  • (string) $image The HTML `img` tag where the attribute should be added.
    Erforderlich: Ja
  • (string) $context Additional context to pass to the filters.
    Erforderlich: Ja
Gibt zurück:
  • (string) Converted `img` tag with `loading` attribute added.
Definiert in:
Codex:

Adds `loading` attribute to an `img` HTML tag.



Quellcode

function wp_img_tag_add_loading_attr( $image, $context ) {
	/**
	 * Filters the `loading` attribute value. Default `lazy`.
	 *
	 * Returning `false` or an empty string will not add the attribute.
	 * Returning `true` will add the default value.
	 *
	 * @since 5.5.0
	 *
	 * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
	 *                             the attribute being omitted for the image. Default is `lazy`.
	 * @param string      $image   The HTML `img` tag to be filtered.
	 * @param string      $context Additional context about how the function was called or where the img tag is.
	 */
	$value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );

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

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

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

	return $image;
}