Skip to content

Instantly share code, notes, and snippets.

@fribibb
Forked from onnimonni/rename-uploads.php
Last active June 28, 2022 08:11
Show Gist options
  • Save fribibb/c9d440079590554c063532fed96fc45a to your computer and use it in GitHub Desktop.
Save fribibb/c9d440079590554c063532fed96fc45a to your computer and use it in GitHub Desktop.
Remove accents from Wordpress uploads. This renames all files and replaces corresponding attachment in WordPress database. This helps to avoid possible problems with *EXITSTING* filenames. To avoid this in future install mu-plugin which hooks in 'sanitize_file_name'
<?php
/**
* Replace-uploads.php
* This replaces all accents from your uploads
* you can run this with wp-cli: $ php wp-cli.phar eval-file rename-uploads.php
*
* You may need to run the outputted move commands in order to update the filenames.
*/
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
// Bail.
exit;
}
// Change to right path before making changes.
if ( defined( 'WP_CONTENT_DIR' ) ) {
chdir( WP_CONTENT_DIR );
} else {
chdir( ABSPATH );
}
// Get all uploads.
$uploads = get_posts(
array(
'post_type' => 'attachment',
'numberposts' => -1,
'include' => array( _______POST_IDS_HERE________ ), // TODO.
)
);
global $wpdb;
// Get upload path.
$path = wp_upload_dir()['basedir'];
echo 'Found: ' . count( $uploads ) . " attachments...\n";
echo "This may take a while...\n\n";
// Loop through all upload images.
foreach ( $uploads as $upload ) {
// Reset flag.
$replace_name = false;
foreach ( get_post_meta( $upload->ID, '_wp_attached_file', false ) as $index => $file ) {
echo "# Processing ID: {$upload->ID}\n";
$full_path = $path . '/' . $file;
$ascii_file = strtolower( preg_replace( '/[^a-zA-Z0-9\/\-\_\.]/', '', ( remove_accents( $file ) ) ) );
$ascii_full_path = $path . '/' . $ascii_file;
// This replaces main file.
if ( $file !== $ascii_file ) {
$replace_name = true;
echo "mv '{$full_path}' {$ascii_full_path}\n";
// NOTE: This doesn't seem to work in some cases.
rename( utf8_decode( $full_path ), $ascii_full_path );
// Also replace the thumbnails versions.
$file_path = dirname( $full_path );
$metadata = get_post_meta( $upload->ID, '_wp_attachment_metadata', false )[ $index ];
// Update main file.
$metadata['file'] = $ascii_file;
if ( isset( $metadata['sizes'] ) ) {
foreach ( $metadata['sizes'] as $name => $thumbnail ) {
$metadata['sizes'][ $name ]['file'];
$thumbnail_path = $file_path . '/' . $thumbnail['file'];
$ascii_thumbnail = strtolower( preg_replace( '/[^a-zA-Z0-9\/\-\_\.]/', '', ( remove_accents( $thumbnail['file'] ) ) ) );
// Update metadata on thumbnail so we can push it back to database.
$metadata['sizes'][ $name ]['file'] = $ascii_thumbnail;
$ascii_thumbnail_path = $file_path . '/' . $ascii_thumbnail;
echo "mv {$thumbnail_path} {$ascii_thumbnail_path}\n";
// NOTE: This doesn't seem to work in some cases.
rename( utf8_decode( $thumbnail_path ), $ascii_thumbnail_path );
}
}
$fixed_metadata = serialize( $metadata );
$file_basename = basename( $file );
$replace_file_basename = basename( $ascii_file );
// Replace in DB...
// Replace main file.
$sql = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '{$ascii_file}' WHERE post_id={$upload->ID} and meta_key='_wp_attached_file';";
$wpdb->query( $sql );
// Replace thumbnail fields.
$sql = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '{$fixed_metadata}' WHERE post_id={$upload->ID} and meta_key='_wp_attachment_metadata';";
$wpdb->query( $sql );
// Space out each set of commands.
echo "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment