Filter to modify post_title after image upload?

When I upload images, WordPress 3.4.2 sets the Title to the base of the filename, such as “DSCN1234” or “IMG_1234”. I’d like the Title to be left blank.

The code that sets the Title during the upload seems to be in “wp-admin/includes/media.php” in the function media_handle_upload(), here:

Read More
// Construct the attachment array
$attachment = array_merge( array(
   'post_mime_type' => $type,
   'guid' => $url,
   'post_parent' => $post_id,
   'post_title' => $title,
   'post_content' => $content,
), $post_data );

If I just change it to 'post_title' => "", it fixes it, but I understand that the core code shouldn’t be modified.

Is there a filter I can use to modify the Title after it’s been set by the upload handler? I tried wp_handle_upload and wp_handle_upload_prefilter but they don’t give me access to the post_title data.

The filter attachment_fields_to_edit does give me access to post_title, but it’s fired for every image when editing a gallery — I just want to modify the post_title of a single image immediately after it’s uploaded. (if the user manually sets the Title to the filename afterward, I don’t want to remove it.) Any other ideas?


UPDATE: well, I just discovered that the post_title is set back to the file name every time the gallery is updated (re-sorting images, etc.) And even if I use the attachment_fields_to_edit filter to clear the post_title, it doesn’t stay cleared because when I click Save Changes, something is apparently not liking the fact that I’ve set the Title field to be empty even though it’s a required field (red asterisk next to it). Other ideas appreciated.

Thanks!
Russell

Related posts

Leave a Reply

3 comments

  1. (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.

  2. What could be done is set the title of all uploads to “Untitled”.

    Then, filter the_title and return an empty string if the attachent title matches the default.

    The basic idea is:

    add_action( 'add_attachment', 'wpse_70093_modify_uploaded_file_title' );
    add_filter( 'the_title', 'wpse_70093_display_untitled', 10, 2 );
    
    function wpse_70093_modify_uploaded_file_title( $attachment_ID ) 
    {
        $the_post = array();
        $the_post['ID'] = $attachment_ID;
        $the_post['post_title'] = 'Untitled';
        wp_update_post( $the_post );
    }
    
    function wpse_70093_display_untitled( $title, $id )
    {
        if( 'attachment' != get_post_type( $id ) )
            return $title;
    
        if( is_admin() )
            return $title;
    
        if( !is_admin() && 'Untitled' == $title ) 
            return '';
    
        return $title;
    }
    

    [update]
    I was missing the frontend approach. Filtering the_title only works for the attachment.php page.

    The images inserted through the editor have to be filtered with:

    add_filter('image_send_to_editor', 'wpse_53577_img_wrapper', 20, 8);
    
    function wpse_53577_img_wrapper($html, $id, $caption, $title, $align, $url, $size, $alt) 
    {
        /* Manipulate $html result */
        return $html;
    }
    

    The [gallery] shortcode has to be rebuild with post_gallery.

    Other cases have to be dealt with individually.