Filter to modify post_title after image upload?

(Answering my own question, with help from @brasofilo)

WordPress 3.5 has a great new Media manager, and it no longer requires the Title to be filled in for images. It also no longer fills in the Title automatically when reorganizing images in a Gallery. It does, however, still fill in the Title with the image filename when uploading the image, such as “DSCN1234”. But this can be prevented by adding the following code to the functions.php file in your theme:

add_action( 'add_attachment', 'wpse_70093_modify_uploaded_file_title' );

function wpse_70093_modify_uploaded_file_title( $attachment_ID ) 
{
    $the_post = array();
    $the_post['ID'] = $attachment_ID;
    $the_post['post_title'] = '';
    wp_update_post( $the_post );
}

After the image is uploaded, the Title will be empty, and it’ll stay that way unless you specifically set it to something – even if the image is edited in WordPress.

Leave a Comment