1
  • When adding an image attachment to a post, the Title field is always filled automatically with the file name.

  • For some images I change this into something more meaningful, for other images I want this field to be empty (in my theme I'm displaying this title field as a caption below each image, but not all images should have this caption).

  • However, if I clear the Title field, it shows again the file name upon save - with the message "Empty Title filled from filename."

  • Any ideas how to change this behaviour, or suggestions for a workaround? I could use the Alt field or Caption field, but I prefer using the Title field - so my client can already fill in the titles by changing the filenames before upload.

3 Answers 3

0

Without a post title, Attachments become significantly harder to use as they no longer have a title that can be clicked on.

If you're processing attachments and generating html manually I recommend using another piece of attachment meta, or just filling in appropriate titles, after all an image is a picture of something, let that something be its name, there's nothing stopping duplicates either.

If you really must remove the title, go to the html tab and manually remove it from the tag. Else put in a placeholder character such as '-'.

Ofnote, attachments are posts too, and that means they have post meta. You could add a checkbox to the edit attachment page, allowing you to toggle on and off wether the attachments title is displayed or not.

7
  • how is not using title attribute generating invalid code? Also your eventual solution is just the same as mine.
    – woony
    Commented Aug 23, 2012 at 14:12
  • Can you provide me with a reference to a blank title being a violation of html spec? I don't really think that's true - a title tag is not an alt tag... From the HTML5 spec: "The title attribute can be used for supplemental information." I agree, however, about your comment about not having a title to click on in the UI - I hadn't thought about this, that's a good explanation of why the title field is mandatory. Maybe I'll just use the Alt field instead of the Title field...
    – ptriek
    Commented Aug 23, 2012 at 14:45
  • Sorry it is the alt attribute that is mandatory, but that doesn't mean you should be able to have a blank title, as it makes the image/media unselectable in the user interface.
    – Tom J Nowell
    Commented Aug 23, 2012 at 15:07
  • @woony Motorolla makes phones, that doesn't stop Nokia and Samsung from trying to outdo them
    – Tom J Nowell
    Commented Aug 23, 2012 at 15:08
  • I've updated my answer with edits
    – Tom J Nowell
    Commented Aug 23, 2012 at 15:12
0

Just a suggestion.

use a specific title or charactar for titles you dont want to show. for example. * or # or idontwanttoshowthistitle

Then in your theme, check if title != * or # or idontwanttoshowthistitle to use it as caption.

1
  • thanks, that could be a workaround - but i prefer a 'cleaner' solution, since it's for a client...
    – ptriek
    Commented Aug 23, 2012 at 14:46
0

I also needed to do this on a site recently.

If you leave the title field empty then WordPress will replace the empty title with the name of the attachment during save. This is done by adding a function to the filter attachment_fields_to_save. If we unhook that filter the attachment can be saved with an empty title:

// Remove WordPress built in filter that filles post name with attachment filename if title is empty on save
// We use prio 5 on our filter because we need to call our function before wordpress calls its own
add_filter( 'attachment_fields_to_save', 'unhook_attachment_fields_to_save', 5, 2 );

function unhook_attachment_fields_to_save( $post, $attachment ) {

    // "image_attachment_fields_to_save" is the name of the function
    // that wordpress did add
    remove_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10 );

    return $post;

}

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.