Advertisement
Guest User

Untitled

a guest
Jan 14th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. function add_image_attachment_fields_to_edit($form_fields, $post) {
  2.   // $form_fields is a an array of fields to include in the attachment form
  3.   // $post is nothing but attachment record in the database
  4.   //     $post->post_type == 'attachment'
  5.   // attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
  6.   // now add our custom field to the $form_fields array
  7.   // input type="text" name/id="attachments[$attachment->ID][custom1]"
  8. $categories = get_categories();
  9. //var_dump($categories);
  10. $form_fields["custom4"]["label"] = __("Custom Select");
  11. $form_fields["custom4"]["input"] = "html";
  12. $form_fields["custom4"]["html"] = "<select name='attachments[{$post->ID}][custom4]' id='attachments[{$post->ID}][custom4]'>";
  13.  
  14. foreach ($categories as $category) {
  15.      $form_fields["custom4"]["html"] .= '<option value="$category">' . $category . '</option>';
  16. }
  17.  
  18. $form_fields["custom4"]["html"] .= "</select>";
  19.    return $form_fields;
  20. }
  21. // now attach our function to the hook
  22. add_filter("attachment_fields_to_edit", "add_image_attachment_fields_to_edit", null, 2);
  23.  
  24. function add_image_attachment_fields_to_save($post, $attachment) {
  25.   // $attachment part of the form $_POST ($_POST[attachments][postID])
  26.         // $post['post_type'] == 'attachment'
  27.   if( isset($attachment['restaurant']) ){
  28.     // update_post_meta(postID, meta_key, meta_value);
  29.     update_post_meta($post['ID'], '_restauarant', $attachment['restaurant']);
  30.   }
  31.   return $post;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement