admin管理员组

文章数量:1126449

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?

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 asked Sep 13, 2011 at 22:25 N2MysticN2Mystic 3,1937 gold badges47 silver badges72 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4

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

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

If you want to filter attachments based on file types, you could modify the meta_query in the code to include an additional condition based on the file extension. The file extension is typically part of the filename, and you can use it to filter attachments. However, keep in mind that relying solely on the file extension for security checks may not be foolproof, as filenames can be manipulated.

Here's an example modification to the code to filter attachments based on a specific file extension (e.g., .jpg):

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

function filename_filter_wpse_28439( $name ) {
// Specify the file extension you want to target
  $target_extension = 'jpg';

  $args = array(
    'numberposts'   => -1,
    'post_type'     => 'attachment',
    'meta_query'    => array(
        'relation' => 'AND',
        array( 
            'key'     => '_wp_attached_file',
            'value'   => $name,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => '_wp_attachment_ext',
            'value'   => $target_extension,
            'compare' => '='
        )
    )
  );

  $attachments_to_remove = get_posts( $args );

  foreach( $attachments_to_remove as $attach )
    wp_delete_attachment( $attach->ID, true );

  return $name;
}

本文标签: theme developmentHow to force Media manager to overwrite files of same name