Get started with 33% off your first certification using code: 33OFFNEW

Image srcsets in WordPress without a plugin

3 min read
Published on 7th March 2024

Blog Image

Ensuring your images look sharp and load quickly across all devices is so important these days. WordPress offers built-in support for responsive images using srcset attributes. Srcsets enable the browser to choose the most appropriate image size based on the user's device, improving your site's performance and user experience. This article guides you through implementing srcsets in WordPress manually, without the need for additional plugins.

Understanding Srcset and Sizes Attributes

Before diving into implementation, let's clarify what srcset and sizes attributes are:

  • Srcset: This attribute allows you to specify multiple image sources for a single <img> element. The browser then selects the most appropriate version based on the current viewport width and pixel density.
  • Sizes: Works in tandem with srcset, telling the browser how much space the image will take up in the viewport at various breakpoints. This allows for more efficient image selection.

Step 1: Enabling Support for Responsive Images

WordPress automatically adds srcset and sizes attributes to images inserted through the WordPress editor. However, ensuring your theme supports responsive images can enhance this functionality.

Add the following line to your theme's functions.php file to enable responsive image support:

add_theme_support( 'responsive-embeds' );

Step 2: Adding Srcset Attributes to Custom Images

While WordPress handles images uploaded via the editor, you might need to manually add srcset attributes to custom images added through theme files or widgets. Here's how you can do it:

  1. Prepare Your Images: Before implementing srcsets, ensure you have multiple sizes of each image. WordPress automatically creates several sizes of each uploaded image. You can find these sizes under Settings > Media in your WordPress dashboard.

  2. Use wp_get_attachment_image_srcset(): WordPress provides this function to generate a srcset string for an image attachment.

Example usage in a theme file:

$image_id = get_post_thumbnail_id( $post_id ); // Get the current post's featured image ID.
$srcset = wp_get_attachment_image_srcset( $image_id, 'full' );
$sizes = '(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 33vw'; // Define sizes.
echo '<img src="' . wp_get_attachment_url( $image_id ) . '" srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '">';

This code retrieves the srcset for the featured image of a post and outputs an <img> tag with proper srcset and sizes attributes.

Step 3: Dynamically Generating Sizes Attributes

For more complex layouts, you might want to dynamically generate the sizes attribute based on the content layout. Here's a basic approach:

  1. Define Your Breakpoints: Determine the breakpoints at which your image layout changes. This could align with your theme's responsive design breakpoints.

  2. Calculate Image Sizes: For each breakpoint, calculate the width of the image as a percentage of the viewport width.

  3. Create a Sizes String: Concatenate these values into a valid sizes attribute.

Example:

$sizes = '(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 25vw';

Step 4: Testing Your Implementation

After implementing srcsets and sizes, it's crucial to test how your images load on different devices and resolutions:

  1. Use Browser Developer Tools: Modern browsers allow you to simulate various devices and screen sizes. Check to see if the correct image is loaded at different viewport sizes.

  2. Validate Your Markup: Use online tools to validate your HTML and ensure your srcset and sizes attributes are correctly formatted.

Conclusion

Implementing srcsets in WordPress without a plugin is a straightforward process that significantly contributes to your site's responsiveness and performance. By leveraging WordPress's built-in functionality and manually adding srcsets to custom images, you ensure that your visitors enjoy fast loading times and crisp visuals, regardless of their device. You do not need a plugin to implement responsive images in WordPress.