Using WordPress ‘load_image_to_edit_attachmenturl’ PHP filter

The load_image_to_edit_attachmenturl WordPress PHP Filter allows you to modify the path to an attachment’s URL when editing the image.

Usage

add_filter('load_image_to_edit_attachmenturl', 'your_custom_function', 10, 3);

function your_custom_function($image_url, $attachment_id, $size) {
  // your custom code here
  return $image_url;
}

Parameters

  • $image_url (string|false): Current image URL.
  • $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_attachmenturl

Examples

Change Image URL to Use CDN

If you want to use a CDN for your images, you can modify the image URL when editing the image.

add_filter('load_image_to_edit_attachmenturl', 'change_image_url_to_cdn', 10, 3);

function change_image_url_to_cdn($image_url, $attachment_id, $size) {
  // Replace 'yourdomain.com' with your actual domain
  // Replace 'cdn.yourdomain.com' with your actual CDN domain
  $image_url = str_replace('https://yourdomain.com', 'https://cdn.yourdomain.com', $image_url);
  return $image_url;
}

Append Timestamp to Image URL

If you want to force a browser cache refresh, you can append the timestamp to the image URL.

add_filter('load_image_to_edit_attachmenturl', 'append_timestamp_to_image_url', 10, 3);

function append_timestamp_to_image_url($image_url, $attachment_id, $size) {
  $timestamp = time();
  $image_url = $image_url . "?t=" . $timestamp;
  return $image_url;
}

Use Placeholder Image for Missing Images

If the image is missing, you can use a placeholder image instead.

add_filter('load_image_to_edit_attachmenturl', 'use_placeholder_for_missing_images', 10, 3);

function use_placeholder_for_missing_images($image_url, $attachment_id, $size) {
  if ($image_url === false) {
    $image_url = 'https://yourdomain.com/path/to/placeholder-image.jpg';
  }
  return $image_url;
}

Change Image URL for a Specific Size

If you want to change the image URL for a specific size, you can do so with this example.

add_filter('load_image_to_edit_attachmenturl', 'change_image_url_for_specific_size', 10, 3);

function change_image_url_for_specific_size($image_url, $attachment_id, $size) {
  if ($size == 'thumbnail') {
    // Custom code to generate new image URL for the thumbnail size
    $image_url = 'https://yourdomain.com/path/to/new-image-url.jpg';
  }
  return $image_url;
}

Change Image URL Based on Attachment ID

If you want to change the image URL for a specific attachment ID, you can do so with this example.

add_filter('load_image_to_edit_attachmenturl', 'change_image_url_for_specific_attachment', 10, 3);

function change_image_url_for_specific_attachment($image_url, $attachment_id, $size) {
if ($attachment_id == 123) {
// Custom code to generate new image URL for attachment ID 123
$image_url = 'https://yourdomain.com/path/to/new-image-url.jpg'; 
} 
return $image_url; 
}