-
-
Save agustinprosperi/4d571777f820b1fc8cd3f3a6ec49a478 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'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment