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
(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: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.
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?)
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:
[update]
I was missing the frontend approach. Filtering
the_title
only works for theattachment.php
page.The images inserted through the editor have to be filtered with:
The
[gallery]
shortcode has to be rebuild withpost_gallery
.Other cases have to be dealt with individually.