Improving Image Handling on an E-commerce Website

Table of Contents
graphic of a laptop with information representing accessibility

As a programmer, we are always looking for ways to improve the user experience on our website. In this devlog, we will be discussing our recent efforts to fine-tune the images on our e-commerce website.

Problem with wp_get_attachment_image_src

The built-in function wp_get_attachment_image_src in WordPress wasn’t cutting it for our needs. It was returning all the thumbnail sizes from WordPress, but we needed more control over the images displayed on our website. This lack of control was causing issues with image quality and user experience.

Importance of Image Quality

Images play a crucial role in the success of an e-commerce website. They provide customers with a visual representation of the products they are considering purchasing, and can heavily influence their decision-making process. Poor image quality can lead to a negative user experience, and ultimately result in lost sales.

Our Approach

To ensure that our website was providing the best possible user experience, we decided to take a closer look at our image handling processes. After evaluating several options, we determined that the best solution was to create a custom solution that would give us more control over the images displayed on our website.

Our Solution


In order to resolve the issues with wp_get_attachment_image_src, we decided to take a different approach to retrieving the images needed for our e-commerce website. Our solution involved creating custom code to more accurately retrieve the images we needed.

Code Implementation

The following code was created to retrieve the images:

$full_size_src = wp_get_attachment_image_src( $feat_image, 'full' );
$single_product_src = wp_get_attachment_image_src( $feat_image, 'woocommerce_single' );
$medium_size_src = wp_get_attachment_image_src( $feat_image, 'medium' );

$img_width = 540;
$img_height = 540;
$img_class = "feat_image image $active";
$img_draggable = "false";
$img_alt = $alt_text;

$srcset = "$medium_size_src[0] 768w, $single_product_src[0] 1128w";

$feat_image = '<img width="'.$img_width.'" height="'.$img_height.'" src="'.$full_size_src[0].'" srcset="'.$srcset.'" class="'.$img_class.'" draggable="'.$img_draggable.'" alt="'.$img_alt.'" />';

echo $feat_image;

The code uses the wp_get_attachment_image_src function to retrieve the image sources, but with different parameters to specify the size of the image needed. The $full_image_src variable retrieves the full size of the image, $single_product_src retrieves the size needed for the single product page, and $thumbnail_src retrieves the thumbnail size.

Next, the code sets the desired width, height, class, draggable attribute, and alt text for the image. These values are stored in variables to make it easier to modify the code in the future if needed. Finally, the $gallery_image variable is used to display the image on the page using the HTML img tag and the stored variables.

By using this custom code, we are able to have more control over the images displayed on our e-commerce website, ensuring that they are displayed in the most optimal way possible.


In the world of e-commerce, providing a positive user experience is crucial for success. This includes having fast loading times, easy navigation, and high-quality images that accurately represent the products being sold. By fine-tuning the images on our website, we are ensuring that our users have the best possible experience while browsing and making purchases.

See more posts like this!

Nolan Phelps
Nolan Phelps

With over a decade of experience in web development and a passion for SEO, I bring a wealth of real-life skills and constant research to the table. I've tackled projects for international hotels, mining corporations, and equipment manufacturers, and I specialize in website performance and speed optimization like no other. I'm here to empower you with my expertise and tips for website success through my blog.

All Posts
Interested? Curious? Bored? Let's start a conversation!