The action hook save_post
is called on save, but i don’t know if you can add metadata at this time. But it should be possible to create/update your meta data after the post was saved with the action hook updated_post_meta
.
EDIT
To pre-select some meta fields (custom fields) on the post creation screen, you have to add these meta values first with an empty value.
If you look at the post_custom_meta_box()
function (which is the callback for the used metabox postcustom
) in the file wp-admin/includes/meta-boxes.php
, you can see that the function is using list_meta()
to create the pre-selected meta fields.
Now lets take a look at the program flow until this metabox is displayed (We’re looking for a action/filter hook we can use here):
- WordPress loads the file
post-new.php
- This file generates a default post in the database on line
39
with the functionget_default_post_to_edit()
. Thats nice. Basically the post is already in the database as an auto-draft. Unfortunately there isn’t any hook at this time to alter these data or add something new. - As a next step, the file
edit-form-advaned.php
is included. This file will generate the hole admin page and includes all required metaboxes based on thesupports
parameter of the post type. - On line
136
the custom fields metaboxpostcustom
is included and the above function is called. Again, no action hook which we could use.
Conclusion
I think the only way you can do is to use jQuery or overload the postcustom
metabox and add the meta values before you run the list_meta()
function.
E.g.
add_action('admin_menu', 'wpse29358_replaceMetaBoxes'); // maybe add_meta_boxes hook
function wpse29358_replaceMetaBoxes() {
remove_meta_box('postcustom', {POST_TYPE}, 'normal');
add_meta_box('postcustom', __('Custom Fields'), 'wpse29358_postcustomMetabox', {POST_TYPE}, 'normal', 'core');
}
function wpse29358_postcustomMetabox($post) {
// Add your meta data to the post with the ID $post->ID
add_post_meta($post->ID, 'key', 'value');
// and then copy&past the metabox content from the function post_custom_meta_box()
}