Using WordPress ‘load_image_to_edit_filesystempath’ PHP filter

The load_image_to_edit_filesystempath WordPress PHP Filter allows you to modify the path to an attachment’s file when editing an image, excluding the ‘full’ image size.

Usage

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

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

Parameters

  • $path string: Path to the current image.
  • $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_filesystempath

Examples

Change the image path based on attachment ID

Modify the image path for a specific attachment ID.

add_filter('load_image_to_edit_filesystempath', 'change_image_path_based_on_id', 10, 3);

function change_image_path_based_on_id($path, $attachment_id, $size) {
    if ($attachment_id == 100) {
        $path = '/new/path/to/image.jpg';
    }
    return $path;
}

Add a watermark to an image

Add a watermark to images with a specific size.

add_filter('load_image_to_edit_filesystempath', 'add_watermark_to_image', 10, 3);

function add_watermark_to_image($path, $attachment_id, $size) {
    if ($size == 'thumbnail') {
        $path = '/path/to/watermarked/image.jpg';
    }
    return $path;
}

Change image path based on size

Change the image path for a specific size.

add_filter('load_image_to_edit_filesystempath', 'change_image_path_based_on_size', 10, 3);

function change_image_path_based_on_size($path, $attachment_id, $size) {
    if ($size == 'medium') {
        $path = '/new/path/to/medium/image.jpg';
    }
    return $path;
}

Change the image path for custom dimensions

Change the image path for custom image dimensions.

add_filter('load_image_to_edit_filesystempath', 'change_image_path_for_custom_dimensions', 10, 3);

function change_image_path_for_custom_dimensions($path, $attachment_id, $size) {
    if ($size == array(300, 200)) {
        $path = '/new/path/to/custom/dimensions/image.jpg';
    }
    return $path;
}

Change the image path for all sizes except ‘full’

Modify the image path for all sizes except ‘full’.

add_filter('load_image_to_edit_filesystempath', 'change_image_path_except_full', 10, 3);

function change_image_path_except_full($path, $attachment_id, $size) {
    if ($size != 'full') {
        $path = '/new/path/to/other/sizes/image.jpg';
    }
    return $path;
}