Ready to get started?Download WordPress


[resolved] Creating new metadata for attachments ? (7 posts)

  1. tambourdeville
    Posted 1 year ago #

    Hi folks!
    I need some help:
    I inserted a checkbox in the picture uploader (after the description of the picture). So each attachment must have his own parameter.

    What I need is to retrieve the value of that checkbox in my function, which is inserted in the filter "media_send_to_editor".

    How can I do that? The only way I see is to create a new metadata for each image. But I can't find how to do that.
    Any help will be appreciated !

  2. CoolArts
    Posted 1 year ago #

    An attachment is a post so you can save a post meta for each image

    To setup you just need:


    And to retrieve it:


    For example:


    Whish it helps.

  3. tambourdeville
    Posted 1 year ago #

    Thanks for the answer.
    Here is my code to add the input in the media uplaod screen:

    function tdv_integration_attachment_fields_to_edit( $form_fields, $post ) {
      if(get_option('hi_enabledForDefault') == 1)
          $checkedHighslide = 'checked="checked" ';>
       $my_form_fields = array(
          'tdv' => array(
             'label'     => __('Enlargement', HI_TEXTDOMAIN),
             'input'     => 'html',
             'html'      => "
                <input type='checkbox' name='tdv-{$post->ID}' id='tdv-{$post->ID}' data-user-setting='tdv-{$post->ID}' data-setting='tdv-{$post->ID}' $checkedHighslide />
                <label for='tdv-{$post->ID}'>" . __('Use tdv', HI_TEXTDOMAIN) . "</label>" )
        if( $post->post_mime_type == 'image/jpeg' OR  $post->post_mime_type == 'image/gif' OR $post->post_mime_type == 'image/png' OR $post->post_mime_type == 'image/tiff')
    	  return array_merge( $form_fields, $my_form_fields );
          return $form_fields;
    add_filter( 'attachment_fields_to_edit', 'tdv_integration_attachment_fields_to_edit', 66, 2 );

    And I can't find a way to retrieve if my input is checked or not...
    Anyone would know ?

  4. vjpo
    Posted 1 year ago #

    add_filter('attachment_fields_to_edit', 'my_image_attachment_fields_to_edit', 10, 2);
    add_filter('attachment_fields_to_save', 'my_image_attachment_fields_to_save', 10, 2); 
    function my_image_attachment_fields_to_edit($form_fields, $post) {
        $value = get_post_meta($post->ID, '_drink', true);
        $form_fields['drink'] = array(
            'label' => __('Drink'),
            'input' => 'html',
            'html'  => "<input type='radio' name='attachments[{$post->ID}][drink]' value='tea' ". (( $value == 'tea' ) ? "checked='checked'" : "" ) . "/> tea
    		    <input type='radio' name='attachments[{$post->ID}][drink]' value='coffee' ". (( $value == 'coffee' ) ? "checked='checked'" : "" ) . "/> coffee",
            'value' => $value,
        return $form_fields;
    function my_image_attachment_fields_to_save($post, $attachment) {
        if( isset($attachment['drink']) )
            update_post_meta($post['ID'], '_drink', $attachment['drink']);
        return $post;

    This works for me. Hope it will help :)

  5. ethanbeyer
    Posted 1 year ago #


    I am using a bit of your code because it was exactly what I was looking for...unfortunately I can't get the radio buttons to actually show up in the Edit Media page. How did you do that?

    Thank you in advance!

  6. ethanbeyer
    Posted 1 year ago #

    Scratch that - I had forgotten to rename the functions to match the filter call. Nevermind. Thanks for sharing this code!

  7. tambourdeville
    Posted 1 year ago #

    @CoolArts & vjpo: many thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic