How to allow empty title for attachments?

  • 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).

    Read More
  • 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.

Related posts

Leave a Reply

3 comments

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

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

  3. 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;
    
    }