8

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?

4

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.

  • ...says the man with the face as white of that of a ghost ;-) – Pieter Goosen Dec 15 '15 at 10:35
  • 1
    The Phantom of the WoPera ;-) @PieterGoosen – birgire Dec 15 '15 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.