I wanted to touch on this tip a little more, as I might have briefly skimmed over it on my previous post: How to Create a Custom WordPress UI (Post Meta Box).
As you begin to build custom UI elements (meta box elements) for your WordPress themes and/or plugins; you may find it useful to hide certain meta data variable names so that they do not appear in the Custom Fields area (these are the values that get stored in the wp_postmeta database table).
There is no point in allowing the user to edit a piece of meta data in both your custom UI and also in the Custom Fields section (IMHO, i think it adds to the clutter and may cause confusion).
Blah, Blah, Blah … Just Tell Me How
Hiding post meta data from the custom fields section is pretty straight forward: by using an underscore to prefix the variable names, WordPress will hide it from appearing in the Custom Fields area.
The bellow is code excerpt from: How to Create a Custom WordPress UI (Post Meta Box), it might be a good idea to view it in full context.
function my_meta_save($post_id)
{
// ... code excerpt, see the rest of the code at:
// http://farinspace.com/how-to-create-custom-wordpress-meta-box/
if ($data) update_post_meta($post_id, '_my_meta', $new_data);
else add_post_meta($post_id, '_my_meta', $new_data, TRUE);
}
Thanks, this is really useful.
Is this best practice?
It seems to be the only blog post i have found about the topic and that seems a little hacky.
Is there other ways to do this?
James, you either have the choice to use the custom fields area as is or you can create custom meta boxes, in my opinion the best practice is to create custom meta boxes.
The tip above basically allows you to hide the inner var names from appearing in the custom fields area, which you really do not need if the end user is going to be interacting with the vars/values in a custom meta box anyway.
Have a look at my WPAlchemy PHP Class which will aid you in meta box development.
Hi Dimas,
I tried removing the underscore _ in front of the name but the fields associated with the metabox don’t show up in the custom fields box, any idea why this might be so?