Page Caching<\/strong><\/h3>\n\n\n\nPage caching creates static HTML versions of your dynamic WordPress pages. When a visitor requests a page, the server delivers the cached HTML file instead of processing PHP and querying the database.<\/p>\n\n\n\n
Popular Page Caching Plugins:<\/strong><\/p>\n\n\n\nPlugin<\/strong><\/td>Best For<\/strong><\/td>Key Features<\/strong><\/td><\/tr>| WP Rocket<\/td> | Beginners<\/td> | One-click setup, automatic optimization<\/td><\/tr> | | W3 Total Cache<\/td> | Advanced users<\/td> | Granular control, multiple cache types<\/td><\/tr> | | WP Super Cache<\/td> | Budget-conscious<\/td> | Free, reliable, straightforward<\/td><\/tr> | | LiteSpeed Cache<\/td> | LiteSpeed servers<\/td> | Server-level integration, ESI support<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nDatabase Caching<\/strong><\/h3>\n\n\n\nDatabase caching stores query results in memory, reducing database load. WordPress makes dozens of database calls per page load, so caching these results provides significant speed improvements.<\/p>\n\n\n\n \/\/ Example: Caching a custom query<\/p>\n\n\n\n $cache_key = ‘popular_posts_’ . md5(serialize($args));<\/p>\n\n\n\n $popular_posts = wp_cache_get($cache_key);<\/p>\n\n\n\n if (false === $popular_posts) {<\/p>\n\n\n\n $popular_posts = new WP_Query($args);<\/p>\n\n\n\n wp_cache_set($cache_key, $popular_posts, ”, 3600); \/\/ Cache for 1 hour<\/p>\n\n\n\n }<\/p>\n\n\n\n Object Caching<\/strong><\/h3>\n\n\n\nObject caching stores database query results, API calls, and complex computations in memory. Redis and Memcached are popular solutions that can reduce database load by 80% or more.<\/p>\n\n\n\n Setting up Redis Object Caching:<\/strong><\/p>\n\n\n\n\n- Install Redis on your server<\/li>\n\n\n\n
- Add the Redis Object Cache plugin<\/li>\n\n\n\n
- Configure the connection in wp-config.php:<\/li>\n<\/ol>\n\n\n\n
define(‘WP_REDIS_HOST’, ‘127.0.0.1’);<\/p>\n\n\n\n define(‘WP_REDIS_PORT’, 6379);<\/p>\n\n\n\n define(‘WP_REDIS_TIMEOUT’, 1);<\/p>\n\n\n\n define(‘WP_REDIS_READ_TIMEOUT’, 1);<\/p>\n\n\n\n define(‘WP_REDIS_DATABASE’, 0);<\/p>\n\n\n\n Browser Caching<\/strong><\/h3>\n\n\n\nBrowser caching tells visitors’ browsers to store static files locally. Instead of downloading CSS, JavaScript, and images on every visit, browsers use cached versions.<\/p>\n\n\n\n Optimal Browser Caching Headers:<\/strong><\/p>\n\n\n\n# .htaccess example<\/p>\n\n\n\n <IfModule mod_expires.c><\/p>\n\n\n\n ExpiresActive On<\/p>\n\n\n\n ExpiresByType text\/css “access plus 1 year”<\/p>\n\n\n\n ExpiresByType application\/javascript “access plus 1 year”<\/p>\n\n\n\n ExpiresByType image\/png “access plus 1 year”<\/p>\n\n\n\n ExpiresByType image\/jpg “access plus 1 year”<\/p>\n\n\n\n ExpiresByType image\/jpeg “access plus 1 year”<\/p>\n\n\n\n ExpiresByType image\/gif “access plus 1 year”<\/p>\n\n\n\n <\/IfModule><\/p>\n\n\n\n Lazy Loading: Load What You Need, When You Need It<\/strong><\/h2>\n\n\n\nLazy loading defers the loading of non-critical resources until they’re needed. Instead of loading every image, video, and iframe when the page loads, lazy loading only loads content as users scroll down.<\/p>\n\n\n\n Native Lazy Loading<\/strong><\/h3>\n\n\n\nWordPress 5.5 introduced native lazy loading for images. It’s automatic for most images, but you can control it:<\/p>\n\n\n\n <!– Force lazy loading –><\/p>\n\n\n\n <img src=”image.jpg” loading=”lazy” alt=”Description”><\/p>\n\n\n\n <!– Disable lazy loading for above-the-fold images –><\/p>\n\n\n\n <img src=”hero-image.jpg” loading=”eager” alt=”Hero image”><\/p>\n\n\n\n Advanced Lazy Loading Implementation<\/strong><\/h3>\n\n\n\nFor more control, you can implement custom lazy loading:<\/p>\n\n\n\n \/\/ Intersection Observer API for lazy loading<\/p>\n\n\n\n const imageObserver = new IntersectionObserver((entries, observer) => {<\/p>\n\n\n\n entries.forEach(entry => {<\/p>\n\n\n\n if (entry.isIntersecting) {<\/p>\n\n\n\n const img = entry.target;<\/p>\n\n\n\n img.src = img.dataset.src;<\/p>\n\n\n\n img.classList.remove(‘lazy’);<\/p>\n\n\n\n imageObserver.unobserve(img);<\/p>\n\n\n\n }<\/p>\n\n\n\n });<\/p>\n\n\n\n });<\/p>\n\n\n\n document.querySelectorAll(‘img[data-src]’).forEach(img => {<\/p>\n\n\n\n imageObserver.observe(img);<\/p>\n\n\n\n });<\/p>\n\n\n\n Plugin Solutions for Lazy Loading<\/strong><\/h3>\n\n\n\nPlugin<\/strong><\/td>Pros<\/strong><\/td>Cons<\/strong><\/td><\/tr>| a3 Lazy Load<\/td> | Lightweight, customizable<\/td> | Limited features<\/td><\/tr> | | Lazy Load by WP Rocket<\/td> | Easy setup, excludes critical images<\/td> | Premium only<\/td><\/tr> | | BJ Lazy Load<\/td> | Free, includes videos\/iframes<\/td> | Less actively maintained<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nLazy Loading Best Practices<\/strong><\/h3>\n\n\n\nDo:<\/strong><\/p>\n\n\n\n\n- Lazy load images below the fold<\/li>\n\n\n\n
- Include proper alt text for accessibility<\/li>\n\n\n\n
- Use placeholder images or blur effects<\/li>\n\n\n\n
- Test on mobile devices<\/li>\n<\/ul>\n\n\n\n
Don’t:<\/strong><\/p>\n\n\n\n\n- Lazy load above-the-fold images<\/li>\n\n\n\n
- Forget to exclude critical CSS background images<\/li>\n\n\n\n
- Implement without fallbacks for JavaScript-disabled browsers<\/li>\n<\/ul>\n\n\n\n
| | | |
| | | |