All you need to do is use a few of the wordpress plugins filters (It would also work if you use them in teh functions.php of your theme). The filters you’ll need to use are: attachment_fields_to_save and attachment_fields_to_edit.
For example, try this:
/* For adding custom field to gallery popup */
function add_image_attachment_fields_to_edit($form_fields, $post) {
// $form_fields is a an array of fields to include in the attachment form
// $post is nothing but attachment record in the database
// $post->post_type == 'attachment'
// attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
// now add our custom field to the $form_fields array
// input type="text" name/id="attachments[$attachment->ID][custom1]"
$form_fields["credit"] = array(
"label" => __("Credit"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_credit", true),
"helps" => __("Help string."),
);
return $form_fields;
}
// now attach our function to the hook
add_filter("attachment_fields_to_edit", "add_image_attachment_fields_to_edit", null, 2);
function add_image_attachment_fields_to_save($post, $attachment) {
// $attachment part of the form $_POST ($_POST[attachments][postID])
// $post['post_type'] == 'attachment'
if( isset($attachment['credit']) ){
// update_post_meta(postID, meta_key, meta_value);
update_post_meta($post['ID'], '_credit', $attachment['credit']);
}
return $post;
}
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "add_image_attachment_fields_to_save", null , 2);
Thank you very much!
Would you know the best way to remove the “Description” field?
Sure, just add:
unset($form_fields['post_content']);
in the above before returning $form_fields;
:
/* For adding custom field to gallery popup */
function add_image_attachment_fields_to_edit($form_fields, $post) {
// $form_fields is a an array of fields to include in the attachment form
// $post is nothing but attachment record in the database
// $post->post_type == 'attachment'
// attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
// now add our custom field to the $form_fields array
// input type="text" name/id="attachments[$attachment->ID][custom1]"
$form_fields["credit"] = array(
"label" => __("Credit"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_credit", true),
"helps" => __("Help string."),
);
unset($form_fields['post_content']);
return $form_fields;
}
Thanks very much for the direct help and code leads!
No problem, glad I could help! 🙂
This has been a really handy post and worked for me to add one new field.
However when i attempted to add a second custom field i the message:
Fatal error: Cannot redeclare add_image_attachment_fields_to_edit() (previously declared in /home2/taskbure/public_html/wp-content/themes/twentyeleven/functions.php:684) in /home2/taskbure/public_html/wp-content/themes/twentyeleven/functions.php on line 729
Line 729 is the } on the line below return $form_fields;
I have checked through there is no code missing from the post above.
Am i unaware of something obvious?
Any help would be appreciated.
Thanks 🙂
Is there a way to get more functionality with custom image fields than a simple text input area? Is there something similar/equivalent to the add_meta_boxes function we can use for post editing?
For instance, I’d like to add a map-based location picker for images (many plugins offer this for posts, likely using the add_meta_boxes hook and some others), but I can’t find a way to do it. The attachment_fields_to_edit/attachment_fields_to_save hooks are not enough for what I wanna do.
this was working fine for me until i installed the file gallery plugin. It still shows the fields but ignores the label & helps variables making it look quite messy!
Is there another way to add inputs to the attachment page?