Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use image_editor_save_pre filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 30, 2023
5 minutes read

image_editor_save_pre filter

Filters the WP_Image_Editor instance for the image to be streamed to the browser.

To use the image_editor_save_pre filter, first you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins prefer to create a custom WordPress Plugin while using hooks so that nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_image_editor_save_pre_defaults which takes 2 parameters, and we registered it using add_filter. The first parameter, image_editor_save_pre, is the name of the hook. The second parameter, modify_image_editor_save_pre_defaults, is the name of the function that needs to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times. The last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook, so you can use remove_filter to remove the image_editor_save_pre filter.

Parameters

    Below are the 2 parameters required to use this hook.

  • $image: (WP_Image_Editor) The image editor instance.
  • $attachment_id: (int) The attachment post ID.

Live Example

apply_filters('image_editor_save_pre', WP_Image_Editor $image, int $attachment_id)

Below is an example of how you can use this hook.

Example 1

This example demonstrates how to modify the image editor instance before it is saved.

function weplugins_modify_image_editor_save_pre_defaults($image, $attachment_id) { 
  // Update the $image variable according to your website requirements and return this variable. You can modify the $image variable conditionally too if you want.
  return $image; 
}
// add the filter
add_filter("image_editor_save_pre", "weplugins_modify_image_editor_save_pre_defaults", 10, 2);

Example 2

To remove a hook callback, use the example below.

remove_filter("image_editor_save_pre", "weplugins_modify_image_editor_save_pre_defaults", 10, 2);

Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

Example 3

This example shows how to conditionally modify the image editor based on attachment ID.

function weplugins_conditionally_modify_image_editor($image, $attachment_id) {
  if ($attachment_id == 123) {
    // Modify $image for this specific attachment
  }
  return $image;
}
add_filter("image_editor_save_pre", "weplugins_conditionally_modify_image_editor", 10, 2);

[pwap_shortcode id=”10″]

Contact Us

If you’re having any trouble using this hook or need customization, please contact us. We’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.