2

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:

// 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

3 Answers 3

4

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

1
  • I would really like to have a plugin or setting for this in one of the upcoming versions of Wordpress. I just don't want to have it in my template-files because it's nothing specially for my template. Commented Mar 22, 2013 at 14:12
0

There is a plugin you could use for this purpose http://wordpress.org/extend/plugins/img-title-removal/ this plugin would hide the title at all (not sure if this is what you want to accomplish?)

1
  • Thanks - I saw that actually, but I don't really want to remove the title attribute completely, I just don't want it to be set to the image filename. If the user chooses to set the title to something, I don't want to remove it.
    – Russell G
    Commented Oct 22, 2012 at 16:32
0

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.

8
  • I think you're really close with this answer. The "add_attachment" function works great, and sets the newly-uploaded image file to have a post_title of "Untitled". However, the second function only gets called for posts, and not for media files. I added a call to error_log() to display the output of get_post_type($id) when the blog is displayed, and it always displays "post", so the media file is never processed. Any other ideas? Thanks!
    – Russell G
    Commented Dec 3, 2012 at 15:56
  • @RussellG, I've modified the display_untitled logic, and did a fast test drive, seems workable, give it a shot.
    – brasofilo
    Commented Dec 3, 2012 at 16:36
  • Thanks, but when I display the blog page with the images on it, the display_untitled function doesn't seem to be called at all for the images, just for the post itself. If I print the return from get_post_type($id) at the top of the function, it always prints "post". Shouldn't it be called for each image somewhere, or am I missing something?
    – Russell G
    Commented Dec 3, 2012 at 20:45
  • @RussellG, answer updated.
    – brasofilo
    Commented Dec 3, 2012 at 21:06
  • Thanks, but when I add the filter for image_send_to_editor, the function doesn't seem to be called at all. I can't find official documentation for image_send_to_editor but it sounds like it's related to editing images. What action on the blog triggers it? What I think I'm looking for is something that modifies the image's "post_title" attribute when it's displayed to the user on a blog page - right?
    – Russell G
    Commented Dec 4, 2012 at 16:49

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.