The load_image_to_edit_path WordPress PHP Filter allows you to modify the path or URL of the current image.
Usage
add_filter('load_image_to_edit_path', 'your_custom_function', 10, 3); function your_custom_function($filepath, $attachment_id, $size) { // your custom code here return $filepath; }
Parameters
- $filepath (string|false): File path or URL to the current image, or false.
- $attachment_id (int): Attachment ID.
- $size (string|int[]): Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
More information
See WordPress Developer Resources: load_image_to_edit_path
Examples
Change Image Path to CDN
Change the image path to use a CDN instead of the local server.
add_filter('load_image_to_edit_path', 'change_image_path_to_cdn', 10, 3); function change_image_path_to_cdn($filepath, $attachment_id, $size) { $cdn_url = 'https://your-cdn-url.com'; $filepath = str_replace(get_site_url(), $cdn_url, $filepath); return $filepath; }
Force HTTPS for Image URLs
Make sure all image URLs use HTTPS.
add_filter('load_image_to_edit_path', 'force_https_for_images', 10, 3); function force_https_for_images($filepath, $attachment_id, $size) { $filepath = set_url_scheme($filepath, 'https'); return $filepath; }
Add Image Versioning
Append a version number to image URLs for cache-busting.
add_filter('load_image_to_edit_path', 'add_image_versioning', 10, 3); function add_image_versioning($filepath, $attachment_id, $size) { $version = '1.0.0'; $filepath = add_query_arg('ver', $version, $filepath); return $filepath; }
Add Custom Watermark
Add a custom watermark to the image URL.
add_filter('load_image_to_edit_path', 'add_custom_watermark', 10, 3); function add_custom_watermark($filepath, $attachment_id, $size) { $watermark = 'your-watermark-image-url'; $filepath = add_query_arg('watermark', urlencode($watermark), $filepath); return $filepath; }
Change Image Format
Change the image format to WebP for better performance.
add_filter('load_image_to_edit_path', 'change_image_format', 10, 3); function change_image_format($filepath, $attachment_id, $size) { $filepath = str_replace('.jpg', '.webp', $filepath); $filepath = str_replace('.jpeg', '.webp', $filepath); $filepath = str_replace('.png', '.webp', $filepath); return $filepath; }