• Resolved tambourdeville

    (@tambourdeville)


    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 !

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.

    Thread Starter tambourdeville

    (@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 🙂

    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!

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

    Thread Starter tambourdeville

    (@tambourdeville)

    @coolarts & vjpo: many thanks for your help!

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