I know this has been asked before but I'm having trouble with it and this seems poorly documented.
My goal:
Create a function that adds multiple custom fields into each item in the media gallery. What I ideally want is that:
- these would only show on audio media items
- these fields would be things like "lyrics", "cover artist", "original artist" so would be ideal if these can appear under the built in Metadata tab on the audio [Fig 1]
- they would also appear in the quick view along with the other metadata. [Fig 2] The code that works already does this but I want to retain this function.
Website details
My website is a wordpress v5.6.2 website with a handful of fairly popular plugins: Content Control by Code Atlantic, Filebird by Ninja Team, Ultimate Member by Ultimate Member, UpdraftPlus backup/restore by Updraftplus.
What I've tried
I've tried a few things, these don't work for me:
- https://code.tutsplus.com/articles/how-to-add-custom-fields-to-attachments--wp-31100 - this tutorial seems excellent, in-depth, and answers my need, but after reading it through several times, and playing with the code a fair amount, I simply can't get it to work in any way.
- Add custom field to attachments in a specific category - this seems good but again, I can't get it to work even after playing with it a bit.
- https://developer.wordpress.org/reference/hooks/attachment_fields_to_edit/ - I've scanned the official documentation but haven't found any detail that indicates how to define a field for only one media type.
- Official documentation for attachment form fields - This is a good breakdown of the functions but doesn't answer my question.
- https://gist.github.com/igorbenic/25de6609eb2ac2557ff48771d6118a4f - A good looking github page with some good code on it but again nothing to answer my question.
- Modify Custom Field in Media Library using JavaScript - I haven't tried this but it looks concentrated on modifying a field that's already there.
- https://www.billerickson.net/wordpress-add-custom-fields-media-gallery/ - haven't tried, concerned by the clause at the top that says it's out of date.
- Can I add custom meta for each image uploaded via media-upload.php? - doesn't work - or at least, doesn't do what I want.
Not tried:
I've found these but after all the failed attempts above I'm not sure these are going to be any better! but for reference:- https://www.wpbeginner.com/wp-tutorials/how-to-add-additional-fields-to-the-wordpress-media-uploader/
- https://www.kevinleary.net/add-custom-meta-fields-media-attachments-wordpress/
What does work:
- How to add a custom field to the media screen (image/gallery)? - the top voted answer on this question is the only example I've got to work so far. It adds a field into every single media gallery item, and I could add some more fields and use it if all else fails but I'd like to get a cleaner solution for this incorporating all 3 points above. This can be seen on Fig 1 and Fig 2 - the new field called "Style"
Can anyone help me make a function that adds fields in as per my 3x points above? I'd appreciate an answer even if it only answers 1 or 2 of the points though, I've put them in order of what's most important.
Screenshots
Fig 1:
Fig 2:
wp_get_attachment_id3_keys
to add custom fields into the Metadata metabox, however, you'll be limited to single-line text fields only (i.e. the<input>
tag). So you should probably just use theattachment_fields_to_edit
hook, if you want to be able to have other field types liketextarea
.textarea
and other field types, is by removing the default metabox and create your own based on the original source. But I didn't mention that before because you'd still need to use theattachment_fields_to_edit
hook to add your fields to the "Attachment details" popup in Fig 2, so I thought, "why bother with the metabox"?