Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a widget that works with my theme that expects images named in a certain way, slide1.png, slide2.png, etc...

However, when the user uploads their own images, named slide1.png, slide2.png, etc to the media library, rather than updating the images with the new ones, WordPress changes the names of the replacement images to slide11.png, slide22.png, etc

Can I set a filter in theme options that tells WP to overwrite existing images without changing the filenames?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Here is something i cooked up which was taken mainly from the plugin Overwrite Uploads but without the extra stuff

add_filter('wp_handle_upload_overrides','noneUniqueFilename');
function noneUniqueFilename($overrides){
    $overrides['test_form'] = false;
    $overrides['unique_filename_callback'] = 'nonUniqueFilenameCallback';
    return $overrides;
}

function nonUniqueFilenameCallback($directory, $name, $extension){
    $filename = $name . strtolower($extension);
    //remove old attachment
    removeOldAttach($filename);

    return $filename;
}

function removeOldAttach($filename){
    $arguments = array(
        'numberposts'   => -1,
        'meta_key'      => '_wp_attached_file',
        'meta_value'    => $filename,
        'post_type'     => 'attachment'
    );
    $Attachments_to_remove = get_posts($arguments);

    foreach($Attachments_to_remove as $a)
        wp_delete_attachment($a->ID, true);
}
share|improve this answer
    
Unfortunately, its not working for me. Images with the same filename, still get uploaded as separate images as before. Does it work for you? –  N2Mystic Sep 15 '11 at 15:54
1  
there's a footnote on the original script's download page that might be relevant or no? "NOTE: You have to make a small change to one of WordPress' core files for this to work. See the FAQ for details." –  N2Mystic Sep 15 '11 at 15:55
    
Bainternet: have you tested it? I'd love to get it to work, but so far, no go. –  N2Mystic Sep 20 '11 at 1:03
    
Yeah my bad i just remembered that i hacked the core file to get this to work. –  Bainternet Sep 20 '11 at 8:26
1  
So, no way to do it without hacking the core files? I need to include this in a shipping theme. Any thoughts? –  N2Mystic Sep 27 '11 at 1:45

I managed to make this work using a bit of @Bainternet's Answer without hacking the core.

This other Q&A was helpful as well: query_posts: how to show all 'meta_value' containing a specific word?

The function wp_unique_filename has the filter sanitize_file_name just at the beginning, so we can hook there and do the checking and removal of the duplicate attachment.

I did basic localhost testings, please test it thoroughly before applying to a live site.

add_filter( 'sanitize_file_name', 'filename_filter_wpse_28439', 10, 1 );

function filename_filter_wpse_28439( $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 )
        wp_delete_attachment( $attach->ID, true );

    return $name;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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