Hello 🙂
The reason I installed it is because I have a lot of images with accented characters, which are not displaying properly, so I need to clean up the accented characters, which is what I understood this plugin does.
Not really. It just renames the filenames depending on the media titles (or the post they are attached to).
And because they’re listed as already renamed, I don’t have the option to click a button to rename them.
Yes, because the filename already matches perfectly the media title I guess. This is why they don’t need to be renamed.
For all I know it may have renamed files that I didn’t want changed.
I don’t believe so. It would only rename if you change the media title.
Any clarification would be great, thank you!
The way the plugin renames can be modified through a filter. Do you know how to program using WordPress a bit? Have a look at this: https://ww.wp.xz.cn/plugins/media-file-renamer/faq/.
If you use this plugin’s filter, you can basically use this little piece of code (and add it anywhere in your website, probably in the functions.php of your theme for example) and work on this $new variable. In your case, it seems like this $new still has the accents. You need to write some code to replace the accents by the characters with no accents and return this $new. Then the plugin will automatically react on this and it will have actually the behavior you need. It’s easy to find code snippet to remove the accents in PHP, like here: http://stackoverflow.com/questions/3371697/replacing-accented-characters-php.
Thread Starter
t4gal
(@t4gal)
I appreciate your answer, that clears things up!
I checked out the links you posted and have tried messing around with the bit of code from your FAQ and the following bit of code from the other link you posted:
function replaceAccent($string, $replacement = '_')
{
$alnumPattern = '/^[a-zA-Z0-9 ]+$/';
if (preg_match($alnumPattern, $string)) {
return $string;
}
$ret = array_map(
function ($chr) use ($alnumPattern, $replacement) {
if (preg_match($alnumPattern, $chr)) {
return $chr;
} else {
$chr = @iconv('ISO-8859-1', 'ASCII//TRANSLIT', $chr);
if (strlen($chr) == 1) {
return $chr;
} elseif (strlen($chr) > 1) {
$ret = '';
foreach (str_split($chr) as $char2) {
if (preg_match($alnumPattern, $char2)) {
$ret .= $char2;
}
}
return $ret;
} else {
// replace whatever iconv fail to convert by something else
return $replacement;
}
}
},
str_split($string)
);
return implode($ret);
}
But I can’t seem to figure out how to make it work. I’m not very experienced in this kind of stuff, so I was just taking some stabs in the dark. I would appreciate any direction you might be able to give as to how to implement these two, or even if the bit of code I posted here is the right thing to use.
Thanks again for your help!
You mean you don’t know how to put and glue all that?
You could try to put the function replaceAccent() in your functions.php.
Then try to add this after it:
add_filter( 'mfrh_new_filename', 'my_anti_accent_filter', 10, 3 );
function my_anti_accent_filter( $new, $old, $post ) {
return replaceAccent( $new );
}
Then look at the list of your media. The plugin should propose you to rename them to non-accentuated filenames (if the above function works of course :p).
Thread Starter
t4gal
(@t4gal)
Thanks again for your answer.
I tried exactly what you said, but unfortunately nothing changed. I still don’t have the option to rename the accented characters. I thought maybe it was just the replaceAccent() function that wasn’t working, so I did a quick search and tried about 10 other bits of code that claimed to do the same thing, but none of them worked. I’m not sure if I’m missing something, doing something wrong, or if none of these bits of code that I’ve found actually work, as this is fairly foreign to me, so I’m not sure where to go from here.
This is exactly what I used, in my themes functions.php file:
function replaceAccent($string, $replacement = '_')
{
$alnumPattern = '/^[a-zA-Z0-9 ]+$/';
if (preg_match($alnumPattern, $string)) {
return $string;
}
$ret = array_map(
function ($chr) use ($alnumPattern, $replacement) {
if (preg_match($alnumPattern, $chr)) {
return $chr;
} else {
$chr = @iconv('UTF8', 'ASCII//TRANSLIT', $chr);
if (strlen($chr) == 1) {
return $chr;
} elseif (strlen($chr) > 1) {
$ret = '';
foreach (str_split($chr) as $char2) {
if (preg_match($alnumPattern, $char2)) {
$ret .= $char2;
}
}
return $ret;
} else {
// replace whatever iconv fail to convert by something else
return $replacement;
}
}
},
str_split($string)
);
return implode($ret);
}
add_filter( 'mfrh_new_filename', 'my_anti_accent_filter', 10, 3 );
function my_anti_accent_filter( $new, $old, $post ) {
return replaceAccent( $new );
}
If I bought the pro version, would I be able to manually go through each image with accented characters and replace them by hand? Would they then be updated in the database and all references in posts and pages?
Thanks again for your help.
Thread Starter
t4gal
(@t4gal)
I can’t seem to get this working, so it looks like I may just need to go the manual route.
Before I buy the pro version to fix my problem, can you confirm the functionality of it? Will I be able to manually rename any image, including ones that have already been “automatically renamed”? I want to make sure I correctly understand how the pro plugin works before I spend the money on it.
Thanks!
Actually I tried the code above and I couldn’t properly test it because in all my cases, the plugin/WordPress removes those accents from the filenames. I have no idea why your install requires you to attempt to do it manually…
Could you contact me at [email protected]? Maybe I can check more about this issue…
About this:
Before I buy the pro version to fix my problem, can you confirm the functionality of it? Will I be able to manually rename any image, including ones that have already been “automatically renamed”?
Yes 🙂 You can override and manually rename anything you like.
Thread Starter
t4gal
(@t4gal)
Thanks for your response, I’ve sent you an email to the address provided.
Thread Starter
t4gal
(@t4gal)
Hi there,
I sent an email about a week ago, and then sent another a few days after, but have received no response. Did you get my emails?
I think for now I’ll just buy the pro version and start manually renaming, but like I said, I have lots of files to do so it would be nice if I could get the automatic renaming to work, just to save me time.
Thanks for your time.