mehrshaddarzi
9/28/2017 - 5:17 PM

[WordPress][WP Rocket] Wrapper function: Applies WP Rocket’s LazyLoad to wp_get_attachment_image()

[WordPress][WP Rocket] Wrapper function: Applies WP Rocket’s LazyLoad to wp_get_attachment_image()

<?php
/**
 * Wrapper function: Applies WP Rocket’s LazyLoad to wp_get_attachment_image()
 *
 * @link https://developer.wordpress.org/reference/functions/wp_get_attachment_image/
 * @link https://github.com/wp-media/wp-rocket/blob/v2.10.9/inc/front/lazyload.php#L24-L47
 * 
 * @param  int           $attachment_id  (Required) Image attachment ID.
 * @param  string|array  $size           (Optional) Image size. Accepts any 
 *                                       valid image size, or an array of width 
 *                                       and height values in pixels (in that order).
 * @param  boolean       $icon           (Optional) Whether the image should be 
 *                                       treated as an icon.
 * @param  string|array  $attr           Attributes for the image markup.
 * @return string                        HTML img element or empty string on failure.
 */
function wp_rocket__wp_get_attachment_image__lazyload( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {

	$image_html = wp_get_attachment_image( $attachment_id, $size, $icon, $attr );

	if( function_exists( 'rocket_lazyload_images' ) ) {
		return rocket_lazyload_images( $image_html );
	}

	return $image_html;
}