1

By default WordPress doesn’t overwrite an existing image, if image is edited using built-in image editor, instead of that new image is created.

For example image file: image-file-name.jpg is edited, (for example simple crop/resize), new file is created on disk with some random characters at the end of file name, like this image-file-name-655f4dgfd4g4f.jpg

New image will be used in post, but old file will stay on disk, but not used at all. I can't see option to delete old filee, or even to list it/see it. You can delete old image(s) only from FTP.

Is there any function (for functions.php) which will tell WP to replace original image with edited image (or to delete original image after editing).

0

What you want is probably something like this.

You need to manually unlink the original image including added sizes.

The WP image editor automatically creates a new image with a new name as you mentioned but not others added through add_image_size() function.

regenerate_added_sizes() in the code below does exactly that:

function replace_original_images( $override, $filename, $image, $mime_type, $post_id ) {
    if ( 'image/jpeg' !== $mime_type && 'image/png' !== $mime_type ) {
        return $override;
    }
    $image_meta = wp_get_attachment_metadata( $post_id );
    $upload_dir = wp_upload_dir();

    $original_file_path = path_join( $upload_dir['basedir'], $image_meta['file'] );

    // 'full dir' includes year and mouth location
    $upload_full_dir = str_replace( basename( $original_file_path ), '', $original_file_path );

    // delete original image
    unlink( $original_file_path );

    // delete other sizes
    foreach ( $image_meta['sizes'] as $size ) {
        unlink( $upload_full_dir . $size['file'] );
    }
    // regenerate added sizes
    function regenerate_added_sizes( $meta_id, $object_id, $meta_key ) {
        if ( '_wp_attachment_metadata' !== $meta_key ) {
            return;
        }
        $image_meta = wp_get_attachment_metadata( $object_id );
        $upload_dir = wp_upload_dir();

        $new_file_path = path_join( $upload_dir['basedir'], $image_meta['file'] );

        // prevent infinite loops
        remove_action( 'updated_post_meta', 'regenerate_added_sizes' );

        update_post_meta( $object_id, $meta_key, wp_generate_attachment_metadata( $object_id, $new_file_path ) );
    }
    add_action( 'updated_post_meta', 'regenerate_added_sizes', 10, 3 );

    return $override;
}
add_filter( 'wp_save_image_editor_file', 'replace_original_images', 10, 5 );
  • Welcome to WPSE. A quality answer will include some explanation as to why this solves the problem. – butlerblog May 23 at 13:30
  • I added a simple explanation. How's that. – Taku Yoshi May 24 at 3:52
  • Yes - that's much better ;-) – butlerblog May 24 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.