//覆蓋相同檔名的圖片 不會重命名新圖
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;
}
最後修改日期: 2021 年 11 月 26 日

作者