Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save taricco/47272b87d9ee8b876b48efd91ac95a3a to your computer and use it in GitHub Desktop.

Select an option

Save taricco/47272b87d9ee8b876b48efd91ac95a3a to your computer and use it in GitHub Desktop.
add_filter( 'wp_handle_upload_prefilter', function ( $file ) {
$uploads = wp_upload_dir();
$use_yearmonth = get_option( 'uploads_use_yearmonth_folders' );
if ( boolval( $use_yearmonth ) ) {
// if upload to year month based folders is enabled check current target
$year = date( 'Y' );
$month = date( 'm' );
$target = $uploads['path'] . DIRECTORY_SEPARATOR . $year . DIRECTORY_SEPARATOR . $month . DIRECTORY_SEPARATOR . $file['name'];
} else {
// uploads dir
$target = $uploads['path'] . DIRECTORY_SEPARATOR . $file['name'];
}
if ( file_exists( $target ) ) {
$file['error'] = 'File with the same name already exists. Either overwrite/replace the file via FTP, or rename your file before uploading. Remember to FTP overwrite/replace the @2x version of the image file if needed.';
}
return $file;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment