Replace/Overwrite Media upload without loosing post thumbnails
Try to check this code:
// Force Media manager to overwrite files of same name?
add_filter( 'sanitize_file_name', 'filename_filter_wpse_28439', 10, 1 );
function filename_filter_wpse_28439( $name ) {
// Get all attachments with same name
$args = array(
'numberposts' => -1,
'post_type' => 'attachment',
'meta_query' => array(
array(
'key' => '_wp_attached_file',
'value' => $name,
'compare' => 'LIKE'
)
)
);
$attachments_to_remove = get_posts( $args );
foreach( $attachments_to_remove as $attach ) {
// Check all posts with this attacment
query_posts('post_type=any&meta_key=_thumbnail_id&meta_value='.$attach->ID);
if (have_posts()) :
while (have_posts()) : the_post();
// Set temporal marker
update_post_meta( get_the_ID(), '_temp_thumbnail_id', $name );
endwhile;
endif;
// Delete old attachment
wp_delete_attachment( $attach->ID, true );
}
return $name;
// Continue and upload new attachment with simple name
}
// Set new attachment to posts
add_action("add_attachment", 'attachment_manipulation');
add_action("edit_attachment", 'attachment_manipulation');
function attachment_manipulation($post_ID) {
$file = get_attached_file($post_ID);
$path = pathinfo($file);
$filename = $path['filename'].'.'.$path['extension'];
// Check all posts for temporal marker
query_posts('post_type=any&meta_key=_temp_thumbnail_id&meta_value='.$filename);
if (have_posts()) :
while (have_posts()) : the_post();
// If found marker set postthumb with new attachment
update_post_meta( get_the_ID(), '_thumbnail_id', $post_ID );
// Cleanup temporal marker
delete_post_meta( get_the_ID(), '_temp_thumbnail_id', $post_ID );
endwhile;
endif;
}
Is it useful, test for bugs, any suggestions?
More discussions:
WP 3.3 > Still no option to enable automatic image overwrites?
https://wordpress.stackexchange.com/questions/37152/wp-3-3-still-no-option-to-enable-automatic-image-overwrites
Hot to make on Upload New Media to replace files with same name?
https://www.ads-software.com/support/topic/hot-to-make-on-upload-new-media-to-replace-files-with-same-name?replies=10
How do I replace images without FTP?
https://www.ads-software.com/support/topic/how-do-i-replace-images-without-ftp?replies=8
How Do I Stop Incremental Naming In Media Library?
https://en.forums.wordpress.com/topic/how-do-i-stop-incremental-naming-in-media-library
Which filters or actions to use after a media upload and delete?
https://wordpress.stackexchange.com/questions/56434/which-filters-or-actions-to-use-after-a-media-upload-and-delete
Change attachment filename
https://wordpress.stackexchange.com/questions/30313/change-attachment-filename
How to force Media manager to overwrite files of same name?
https://wordpress.stackexchange.com/questions/28439/how-to-force-media-manager-to-overwrite-files-of-same-name
Enable Media Replace – Replace Images Instead Of Deleting Them And Re-Uploading
https://www.wphub.com/enable-media-replace-wordpress-plugin/
Automatically replace uploaded image with smaller one in WordPress
https://random.kerola.nu/2010/09/08/automatically-replace-uploaded-image-with-smaller-one-in-wordpress/