I'm looking for a plugin or sth to let the admin enter a list of specs (name & value) for a given type of post. The regular custom fields would work perfectly but of course, they are used by some other plugins and I don't really want to filter them on my theme (since plugins may be added in the future) and also don't like the admin seeing all that irrelevant/confusing info.
1 Answer
Custom fields are also known as post meta. Many things are stored as post meta, such as the ID of the featured image. You'll have noticed that these do not show up in the custom fields section of your posts.
This is because these post meta values have keys beginning with an underscore. Similarly any post meta you set that also has an underscore at the start, will not appear in the custom fields section.
So your task now is to implement the User Interface for the options you want to present to the user. You would do this using custom meta boxes. Luckily this question and many variants of it have been asked by people wanting to create different kinds of meta box. For this reason I won't go in to detail on that here when others have already done so.
This question and answer will give you the basic gist of what to do to create a custom meta box and how to save the value when the post is saved, but others may provide more information or cater to your specific UI needs.
A final note, when registering a metabox, you have to specify where it goes and what capability is needed to view it. This way you can show the box only to those users with the necessary access. For example, administrators and super administrators have the manage_options
capability, but not editors. Users can also turn the boxes on and off using the screen options, or collapse them down and rearrange them. You do not need to do anything extra to get these UI features.
<dl>
. Some plugins don't prefix their metadata with an underscore and they show up in the out-of-the-box custom fields meta box. I want to create an extra meta box, with the same functionality, but without these plugin-created meta data.