admin管理员组文章数量:1426918
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 badges1 Answer
Reset to default 1What 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 );
本文标签: Replace original imageWP image editor
版权声明:本文标题:Replace original image, WP image editor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468234a2659628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论