Prevent WordPress from adding image’ title automatically

You can try the following to clear the image attachment’s title when it’s inserted but not updated:

/**
 * Empty the image attachment's title only when inserted not updated
 */
add_filter( 'wp_insert_attachment_data', function( $data, $postarr )
{
    if( 
        empty( $postarr['ID'] ) 
        && isset( $postarr['post_mime_type'] )
        && wp_match_mime_types( 'image', $postarr['post_mime_type'] ) 
    )
        $data['post_title'] = '';

    return $data;
}, 10, 2 );

Here we use the wp_insert_attachment_data filter to override the attachment’s title if the attachment’s ID is empty and the mime type is an image type according to wp_match_mime_types(). A simple 'image' === substr( $postarr['post_mime_type'], 0, 5 ) check might work as well. You could even target a given mime type, like image/jpeg.

Leave a Comment