How to Lazy Load Images in WordPress for Faster Pages
To lazy load images in WordPress, use the built-in
loading="lazy" attribute on <img> tags or install a plugin like Lazy Load by WP Rocket. This delays image loading until they are near the viewport, speeding up page load times.Syntax
Lazy loading images in WordPress involves adding the loading="lazy" attribute to the <img> tag. This tells the browser to wait to load the image until it is close to being visible on the screen.
<img src="image.jpg" loading="lazy" alt="Description">: Basic syntax for lazy loading an image.src: The image URL.loading="lazy": Enables lazy loading.alt: Describes the image for accessibility.
html
<img src="image.jpg" loading="lazy" alt="Description">
Output
An image that loads only when near the viewport, improving page speed.
Example
This example shows how WordPress automatically adds lazy loading to images in posts using the loading="lazy" attribute. You can also use a plugin to enhance this behavior.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Lazy Load Images Example</title> </head> <body> <h1>Lazy Loading Images in WordPress</h1> <p>Scroll down to see images load as they come into view.</p> <img src="https://via.placeholder.com/600x400" loading="lazy" alt="Placeholder Image 1"> <div style="height: 1000px;"></div> <img src="https://via.placeholder.com/600x400" loading="lazy" alt="Placeholder Image 2"> </body> </html>
Output
A webpage with two images that load only when scrolled near them, reducing initial load time.
Common Pitfalls
Common mistakes when lazy loading images in WordPress include:
- Not using the
loading="lazy"attribute or disabling it accidentally. - Using plugins that conflict with WordPress's native lazy loading.
- Lazy loading images that are critical for above-the-fold content, causing visible delays.
- Forgetting to add
alttext, which hurts accessibility.
Always test your site after enabling lazy loading to ensure images appear correctly and promptly.
html
<!-- Wrong: Missing lazy attribute --> <img src="image.jpg" alt="Description"> <!-- Right: With lazy loading --> <img src="image.jpg" loading="lazy" alt="Description">
Quick Reference
Tips for lazy loading images in WordPress:
- WordPress 5.5+ adds
loading="lazy"automatically to images. - Use plugins like Lazy Load by WP Rocket for more control and features.
- Exclude important images above the fold from lazy loading to avoid delays.
- Always include descriptive
alttext for accessibility. - Test on different devices and browsers to ensure smooth loading.
Key Takeaways
Use the
loading="lazy" attribute on <img> tags to enable lazy loading in WordPress.WordPress 5.5 and later automatically adds lazy loading to images in posts and pages.
Plugins like Lazy Load by WP Rocket offer enhanced lazy loading features and customization.
Avoid lazy loading images that appear immediately on page load to prevent visible delays.
Always include
alt text for images to maintain accessibility.