Replace original image, WP image editor

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

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).

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).

Share Improve this question asked Aug 14, 2017 at 10:40 Advanced SEOAdvanced SEO 6892 gold badges16 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745468234a4629012.html

相关推荐

  • Replace original image, WP image editor

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

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信