Support » Alpha/Beta/RC » hook after saving an image in the editor

  • Resolved nosilver4u


    I was originally posting this in Hacks, but as it seems this could be an unintentional regression, I’m posting here (feel free to move it if I’m wrong).
    I’ve written a plugin that optimizes images when they are uploaded. However, if a user edits an image after uploading it, the new image does not get optimized. I discovered an ‘edit_attachment’ action hook that almost works, but if the user clicks Save, but not Update, then the hook doesn’t fire.

    Then, I found this:

    However, it seems to have disappeared in 3.5 with the new WP_Image_Editor class.
    Is this the hook I’m looking for? If so, can it be added back in? If not, is there another usable hook?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer



    Try it with the add_attachment hook:

    this is from the function wp_insert_attachment():

    if ( $update) {
    		do_action('edit_attachment', $post_ID);
    	} else {
    		do_action('add_attachment', $post_ID);

    That’s the exact code I was looking at. I already have the optimize process hooked onto when an attachment is added/uploaded (actually right after the metadata is generated), so the add_attachment hook doesn’t do anything new for me.

    I’m specifically looking to re-optimize after a user goes into the WordPress Image Editor, and clicks save/crop/scale (anything that causes the image to be modified) WITHOUT clicking the Update button.

    What hook(s) were you previously using?

    It’s intentional that there is no longer direct access to the GD Resources of images that are in the editor.

    Instead, while not quite the same thing, there are new hooks that will return WP_Image_Editor objects that you can perform operations on:

    • image_save_pre is now image_editor_save_pre
    • wp_save_image_file is now wp_save_image_editor_file
    • image_edit_before_change is now wp_image_editor_before_change

    The reason behind this is that GD had to be abstracted out in order to support other Image Manipulation engines (like Imagick).

    The cool thing here is that it means you can also add your own engine now (to support things like jpegtran or OptiPNG natively).

    More in-depth tutorials on using the API are yet to come, but there’s now a quick intro here (including the filter changes noted above):

    WP_Image_Editor is incoming!

    I wasn’t previously using any hook, so edited images never get optimized unless the user manually optimizes them.

    The one I thought might do the trick was wp_save_image_file, but I couldn’t find it in 3.5, so you answered part of my problem. Unfortunately, that hook is too early in the process.

    My plugin waits until the metadata has been generated for an image, so that we can figure out filenames for thumbnails and other resizes and optimize those as well. When the wp_save_image_editor_file filter is run, the metadata has not yet been updated. So I’m apparently back to the beginning of trying to find an applicable hook. If anyone has ideas for that, please please let me know.

    All that said, you seem to have introduced a new solution going forward. If I understand correctly, my plugin could now run the optimizers directly on the images when they are created including thumbnails/resizes. I’ll be looking forward to that tutorial, but hopefully I can find a solution for those still using old versions of wordpress too.

    Great, glad to hear it!

    Indeed, WP_Image_Editor should let you do just that, for 3.5+ either through extending the existing editors, or by creating a few — one for each of the backends you want to support.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘hook after saving an image in the editor’ is closed to new replies.