Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onnimonni/a2b63eca2f02d6b1ae18 to your computer and use it in GitHub Desktop.
Save onnimonni/a2b63eca2f02d6b1ae18 to your computer and use it in GitHub Desktop.
Run this file with wp-cli: $ wp eval-file remove-accents-uploaded-files-and-database.php to remove accents from all of your previous uploads. Also fix OS-X NFD filenames. After running this command install https://gist.github.com/onnimonni/d58bdcff44f8208a15c7 to your mu-plugins to avoid this in future uploads.
<?php
/**
* Sanitize accents from Cyrillic, German, French, Polish, Spanish, Hungarian, Czech, Greek, Swedish
* This replaces all accents from your uploads by renaming files and replacing attachment urls from database.
* This even removes NFD characters from OS-X by using Normalizer::normalize()
* This is one time script which you can use by running it with wp-cli:
* $ wp eval-file remove-accents-uploaded-files-and-database.php
*/
// Get all uploads
$uploads = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
));
function onnimonni_sanitize_accents($string) {
# If available remove NFD characters
if(class_exists('Normalizer')) {
$ascii_string = remove_accents(Normalizer::normalize($string));
} else {
$ascii_string = remove_accents($string);
}
return $ascii_string;
}
// Replace mysql later
global $wpdb;
// Get upload path
$path = wp_upload_dir()['basedir'];
echo "Found: ".count($uploads)." attachments...\n\n";
echo "This may take a while...\n";
foreach ($uploads as $upload) {
echo "Processing upload (ID:".$upload->ID."): {$upload->guid}";
if (! is_array($upload->$guid) ) {
echo "\nskipping {$upload->post_title}...\n";
}
foreach ($upload->$guid["_wp_attached_file"] as $index => $file) {
$full_path = $path.'/'.$file;
$ascii_file = onnimonni_sanitize_accents($file);
$ascii_guid = onnimonni_sanitize_accents($upload->guid);
$ascii_full_path = $path.'/'.$ascii_file;
// This replaces main file
if ( $file != $ascii_file || $ascii_guid != $upload->guid ) {
echo " ---> Replacing to: {$ascii_full_path}\n";
# Move the file
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;
// Usually this is image but if this is document instead it won't have different sizes
if (isset($metadata['sizes'])) {
foreach ($metadata['sizes'] as $name => $thumbnail) {
$metadata['sizes'][$name]['file'];
$thumbnail_path = $file_path.'/'.$thumbnail['file'];
$ascii_thumbnail = onnimonni_sanitize_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);
##
# Replace Database
##
## Replacing guid
echo "Replacing attachment guid in database...\n";
$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET guid = %s WHERE ID=%d;",$ascii_guid,$upload->ID);
$wpdb->query($sql);
## Replace upload name
echo "Replacing file name in database...\n";
$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}postmeta SET meta_value = %s WHERE post_id=%d and meta_key='_wp_attached_file';",$ascii_file,$upload->ID);
$wpdb->query($sql);
## Replace meta data likethumbnail fields
echo "Replacing thumbnail names in database...\n";
$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}postmeta SET meta_value = %s WHERE post_id=%d and meta_key='_wp_attachment_metadata';",$fixed_metadata,$upload->ID);
$wpdb->query($sql);
} else {
echo "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment