Skip to content

Instantly share code, notes, and snippets.

@onnimonni
Created February 25, 2016 14:20
Show Gist options
  • Save onnimonni/e06a933cdb1b67308262 to your computer and use it in GitHub Desktop.
Save onnimonni/e06a933cdb1b67308262 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. You probably use linux in production and might run OS X in development. This helps to avoid possible problems with 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: $ wp eval-file replace-uploads.php
*/
/**
* cd to right path before 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,
));
global $wpdb;
// Get upload path
$path = get_option('upload_path');
echo "Found: ".count($uploads)." attachments...\n\n";
echo "This may take a while...\n";
foreach ($uploads as $upload) {
$replace_name = false;
foreach ($upload->$guid['_wp_attached_file'] as $index => $file) {
$full_path = $path.'/'.$file;
echo "Processing upload (ID:{$upload->ID}): {$full_path}";
$ascii_file = remove_accents($file);
$ascii_full_path = $path.'/'.$ascii_file;
// This replaces main file
if ($file != $ascii_file) {
$replace_name = true;
echo "---> Replacing to: {$ascii_full_path}\n";
rename($full_path, $ascii_full_path);
# Replace thumbnails too
$file_path = dirname($full_path);
$metadata = unserialize($upload->$guid['_wp_attachment_metadata'][$index]);
// Correct main file
$metadata['file'] = $ascii_file;
foreach ($metadata['sizes'] as $name => $thumbnail) {
$metadata['sizes'][$name]['file'];
$thumbnail_path = $file_path.'/'.$thumbnail['file'];
$ascii_thumbnail = 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 "Processing thumbnail: {$thumbnail_path} ---> {$ascii_thumbnail_path}\n";
rename($thumbnail_path, $ascii_thumbnail_path);
}
$fixed_metadata = serialize($metadata);
$file_basename = basename($file);
$replace_file_basename = basename($ascii_file);
# Replace Database
## Replace upload name
echo "Replacing file name in database...\n";
$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
echo "Replacing thumbnail names in database...\n";
$sql = "UPDATE {$wpdb->prefix}postmeta SET meta_value = '{$fixed_metadata}' WHERE post_id={$upload->ID} and meta_key='_wp_attachment_metadata';";
$wpdb->query($sql);
} else {
echo "\n";
}
}
}
@jukefr
Copy link

jukefr commented Aug 3, 2018

The script worked to change PHP and SQL stuff but left the file names unchanged on my host.

I crafted a little bash script to fix filenames on linux :

find . -type f > uncleaded_list.txt; wp eval "echo remove_accents(\"$(find . -type f)\");" > cleaned_list.txt; paste uncleaded_list.txt cleaned_list.txt > list.txt; xargs -a list.txt -n 2 mv

you need wp cli ofcourse

@Julianoe
Copy link

If anyone stumbles upon this, be careful, it could break you website

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment