PicPerf with WordPress
I recently tested PicPerf on a WordPress website that had many unoptimized images loaded from a CRM system. Instead of creating a custom solution, I opted for PicPerf, a modern drop-in image optimization service. PicPerf automatically converts and optimizes images into smaller formats like WebP, caches them, and serves them closer to the user. I have already noticed significant improvements in loading times and reduced bandwidth usage.
Here is a snippet to integrate PicPerf with all images loaded through WordPress.
add_filter('wp_get_attachment_image_src', function (array|false $image) {
if ($image && wp_get_environment_type() === 'production') {
$image[0] = 'https://picperf.dev/' . $image[0];
}
return $image;
});
Here's another snippet to add PicPerf to srcset
URLs.
add_filter('wp_calculate_image_srcset', function (array $sources) {
foreach ($sources as &$source) {
if (wp_get_environment_type() === 'production') {
$source['url'] = 'https://picperf.dev/' . $source['url'];
}
}
return $sources;
});