Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Wordpress srcset with ACF Image & lazy Load

// Add this to your functions file.

<?php

// Add title attribute to featured image
add_filter( 'wp_get_attachment_image_attributes', 'tl_add_img_title', 10, 2 );
function tl_add_img_title( $attr, $attachment = null){
  $attr['title'] = get_post( $attachment->ID )->post_title;
  return $attr;
}

// Add support for custom image sizes
function ccd_add_image_sizes() {
  add_image_size( 'header-large', 2048, 1152, true );
  add_image_size( 'header-medium', 1024, 576, true );
  add_image_size( 'header-small', 640, 360, true );
}
add_action( 'after_setup_theme', 'ccd_add_image_sizes' );

// Add srcset and sizing to images
function ccd_responsive_images( $image_id, $image_size, $max_width ) {
	// Check if the image ID is not blank
	if ( $image_id != '' ) {
		// Set the default src image size
		$image_src = wp_get_attachment_image_url( $image_id, $image_size );
		// Set the srcset with various image sizes
		$image_srcset = wp_get_attachment_image_srcset( $image_id, $image_size );
		// Generate the markup for the responsive image
		echo 'src="' . $image_src . '" srcset="' . $image_srcset . '" sizes="(max-width: ' . $max_width . ') 100vw, ' . $max_width . '"';
	}
}

// Change the default max width to 2048
function ccd_max_srcset_image_width() {
  return 2048;
}
add_filter( 'max_srcset_image_width', 'ccd_max_srcset_image_width', 10 , 2 );

// Returns an optimised image based on attachment id
function optimised_image($attachment_id, $size) {

  $image_src = wp_get_attachment_image_src( $attachment_id, $size );

  $image_srcset = wp_get_attachment_image_srcset( $attachment_id, $size );

  $attachment_metadata = wp_get_attachment_metadata($attachment_id);

  $width = $attachment_metadata['width'];
  $height = $attachment_metadata['height'];
  $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', TRUE);
  $title = get_the_title($attachment_id);

  return '<img loading="lazy" src="'.esc_url( $image_src[0] ).'" srcset="'.esc_attr( $image_srcset ).'" sizes="(max-width: 640px) 640px, (max-width: 1024px) 1024px, 2048px" width="'.$width.'" height="'.$height.'" alt="'.$alt.'" title="'.$title.'"  />';
} ?>


// Make sure you ACF feild return type is "Image ID"

// Then call the image like so:

<?php

$my_image = get_field('my_image');

?>

<?php echo optimised_image($my_image, 'large'); ?>



Comment

PREVIOUS NEXT
Code Example
Php :: Generating Random String In PHP Using Brute Force 
Php :: phpmailer valid cert 
Php :: how to generate unique alphanumeric 6 digit code through php myadmin 
Php :: back to same page after changing locale 
Php :: testimonial custom post type and uses shortcode 
Php :: php retrieve data from database and show in text area greeper 
Php :: cake php 2.x group 
Php :: php substring last 4 characters to censure credit card 
Php :: laravel form collective add asterisk 
Php :: Compare current time with another time in PHP 
Php :: wordpress php get menu link page id 
Php :: Dropzone Attachment Required is not working 
Php :: Modificar el pie de página del panel de administración de WordPress 
Php :: PDF Library Persian Language UTF-8 Support mPDF Lib 
Php :: Using Cookie Authentication 
Php :: codeigniter database metadata 
Php :: laravel many to many relationship with pivot table 
Php :: php array reduce 
Php :: symfony how to respond in-memory file 
Php :: woocommerce_default_catalog_orderby desc 
Php :: post with count greater than 1 laravel 
Php :: php how to split square bracket and normal sting in a word or sentence 
Php :: append data to json file php 
Php :: laravel command optional parameter 
Php :: Bundling data mvc php 
Php :: Jaygaah Free Shipping Woocommerce 
Php :: remove nul value aray php 
Php :: header redirect php 
Php :: laravel task scheduler error 
Php :: Php countup from a date 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =