11

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?

2
  • checkout this link : wordpress.stackexchange.com/questions/6135/… Commented Dec 15, 2015 at 9:38
  • @ErezLieberman isn't it about removing existing titles? I don't want to remove the titles set by administrator. I only want to prevent WordPress from adding default one when you upload the image.
    – PolGraphic
    Commented Dec 15, 2015 at 9:43

1 Answer 1

9

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.

2
  • ...says the man with the face as white of that of a ghost ;-) Commented Dec 15, 2015 at 10:35
  • 2
    The Phantom of the WoPera ;-) @PieterGoosen
    – birgire
    Commented Dec 15, 2015 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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