WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Add/Customize Meta fields to Edit Media Attachment Screen

[Resolved] Add/Customize Meta fields to Edit Media Attachment Screen

  • Hello,

    BACKGROUND:
    I am trying to customize the “Edit Media” screen (presented to end author/editor WordPress users) linked below:

    Tumblr Post of sample “Edit Media” box in WordPress 3.2.1

    Out of the box WordPress allows us to edit the “Title”, “Alternative Text”, “Caption”, and “Description” fields. I want to add a “Credit” field and perhaps remove the “Description” field.

    SO FAR I HAVE DISCOVERED
    I’ve been poking around the wp-admin/includes/media.php file, specifically around line 1094 (http://pastebin.com/pi8yMKSH)

    QUESTION
    What is the best way to customize my blog so I can alter this screen? I’m willing to use a plugin, but I’d like to learn something here and would be happy to write my own code. I’m just looking for some pointers.

    Thanks!

    Dan LaSota

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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 🙂

    purplearth

    @purplearth

    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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Add/Customize Meta fields to Edit Media Attachment Screen’ is closed to new replies.
Skip to toolbar