WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] Creating new metadata for attachments ?

[Resolved] Creating new metadata for attachments ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • An attachment is a post so you can save a post meta for each image

    To setup you just need:

    update_post_meta(
        $post->ID,
        Name_of_the_variable,
        Value_of_the_variable
    )

    And to retrieve it:

    get_post_meta(
        $post->ID,
        Name_of_the_variable
    )

    For example:

    update_post_meta(
        $post->ID,
        'my_custom_parameter',
        'my_parameter_value'
    )

    Whish it helps.

    tambourdeville
    Member

    @tambourdeville

    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 );
    	  }
        else
          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 ?

    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 🙂

    ethanbeyer
    Member

    @ethanbeyer

    VJPO,

    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!

    ethanbeyer
    Member

    @ethanbeyer

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

    tambourdeville
    Member

    @tambourdeville

    @coolarts & vjpo: many thanks for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Creating new metadata for attachments ?’ is closed to new replies.