Fix Hyphens in WordPress Media Titles

Share This Post

Minor WordPress version updates are fairly safe to apply. I had never seen a serious bug being introduced in a maintenance release – heck, that’s what the releases are for, right?

That is, until a friend reached out saying that all Media uploads that had spaces in their file names started getting hyphenated titles. Sure, she could manually fix the titles after the upload, but with image-intensive sites, that’s just not practical.

I did some digging and it turns out that this is a known bug introduced in WP v4.6.1. The fix is ready for WP 4.6.2, but as of this writing there is no date scheduled for that release.

There is, however, a workaround in the ticket, and I’m adding it here for your benefit. Just copy the code below and paste it at the end of your theme’s functions.php file. Make sure to paste it before any closing ?> tag. Actually, if your functions.php has a closing ?> tag at the end of the file, feel free to remove it. It’s bad practice to keep them lying around that way.

Kudos to @SergeyBiryukov for the fix.

function wp37989_fix_encoded_attachment_titles( $data ) 
{
    if ( empty( $_FILES ) ) 
    {
        return $data;
    }

    $file = current( $_FILES );
    $ext  = pathinfo( $file['name'], PATHINFO_EXTENSION );
    $name = wp_basename( $file['name'], ".$ext" );

    $data['post_title'] = sanitize_text_field( $name );

    return $data;
}
add_filter( 'wp_insert_attachment_data', 'wp37989_fix_encoded_attachment_titles' );

Remember to remove it after you install WordPress 4.6.2. It won’t break anything if you don’t, but it’s redundant and will just take up some un-necessary CPU cycles.

Cheers!

Vinny

P.S. Check out our Website Care Services and let us help you keep your site up and running.

More To Explore

Documentation
vinny

bbPress Notify (No-Spam) Documentation

bbPress Notify (No-Spam) Documentation The bbPress Notify (No-Spam) is a free plugin that lets you control who gets notifications of new Topics and Replies in

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.