I wrote a plugin that set default title for all non-set (empty) titles and leaves the titles set manually (non-empty).

The problem is when I upload new photos (to gallery), WordPress by default set the title to file’s name. How Can I disable it and force WordPress to use empty string as default image title?

1
1

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 Reply

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