Recently a client informed MontanaB that their newly uploaded WordPress media file titles were suddenly being renamed to a slug format (since early September 2016). For example, an uploaded file named “Website Logo.jpg” was being changed to “website-logo” in the title, when it should be “Website Logo”. While this is correct behavior for file names, it can become an issue for the title, especially if the title is being referenced on the website front end.

After troubleshooting and researching the issue, I found that this was a bug from the most recent WordPress 4.6.1 release where the title was being sanitized along with the file name. You can read more about this change at https://core.trac.wordpress.org/changeset/38538

While the intentions of sanitizing the title is great, the sanitation was a little too aggressive. To correct this behavior we can add in our functions.php the following code:
function wp37989_fix_encoded_attachment_titles( $data, $postarr ){
$basename = pathinfo( $postarr['file'], PATHINFO_BASENAME );
$data['post_title'] = preg_replace("/-/", " ", sanitize_text_field( $basename ));
return $data;
}
add_filter('wp_insert_attachment_data', 'wp37989_fix_encoded_attachment_titles', 10, 2 );

 

Share:

Filed under: Tips