• Resolved schoe

    (@schoe)


    In a plugin
    using the filter hook ‘attachment_fields_to_edit’ I can add some extra fields to the image upload form.
    How can I get them attached to the post as image meta data?

    Michael

Viewing 7 replies - 1 through 7 (of 7 total)
  • What is the plugin? Need reference for code.

    Thread Starter schoe

    (@schoe)

    OK, it’s a plugin like “photojar-base” which I prefer,
    adding something like:

    function ms_attachment_fields_to_edit($form_fields, $post) {
        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
            $form_fields['image-meta'] = array(
               'label' => __('Homepage:'),
    	   'input' => 'html',
    	   'html' => "
    	       <input type='text'
                   name='attachments[$post->ID][image-meta][homepage]'
                   id='attachments[$post->ID][image-meta][homepage]'
    	       size='50' value='http://xyz.de' />"
    	);
        }
        return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'ms_attachment_fields_to_edit', 11, 2);

    Homepage will show in the upload form but is not stored as meta_value in the wp_postmeta.
    Obviously I need another hook. Any ideas?

    Michael

    Thread Starter schoe

    (@schoe)

    Sorry, that seems to be the correct code snipet to get the extra field displayed in the upload form:

    function ms_attachment_fields_to_edit($form_fields, $post) {
        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
            $form_fields['homepage'] = array(
    	    'label' => __('Homepage'),
    	    'input' => 'html',
    	    'html'  => "
    	        <input type='text'
                    name='attachments[$post->ID][homepage]'
                    id='attachments[$post->ID][homepage]'
                    size='50' value='http://' /><br />",
        }
        return $form_fields;
    );
    add_filter('attachment_fields_to_edit', 'ms_attachment_fields_to_edit', 11, 2);

    … but I just can’t find the HOOK to get it stored as meta_value in the wp_postmeta!

    Anybody who can help ???

    Michael

    Thread Starter schoe

    (@schoe)

    Just in case anybody is interested:
    The corect hook – of course – is attachment_fields_to_save

    function ms_image_attachment_fields_to_save($post, $attachment) {
        if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
            $post_id = $post['ID'];
            $meta = wp_get_attachment_metadata( $post_id );
            $meta['image_meta']['Homepage'] = $attachment['homepage'];
            wp_update_attachment_metadata( $post_id,  $meta );
        }
        return $post;
    }
    add_filter('attachment_fields_to_save', 'ms_image_attachment_fields_to_save', 11, 2);

    If you know it’s rather simple 🙂

    Michael

    What’s the 11 and 2 for i.e. the 3rd and 4th parameters for add_filter()?

    Thanks in advance.

    Thread Starter schoe

    (@schoe)

    Sorry Mike,

    didn’t check this thread for quite a while

    if you are still interested:

    • the 11 is the $priority
    • the 2 is the $accepted_args

    just visit: http://codex.wordpress.org/Function_Reference/add_filter

    good luck

    Michael

    I modified the above script, I had to give a client the ability to organize images within media library into certain categories, which are used later in the template to assist with layout.

    Ive only tested this script on 2.7, and it is very rough, but perhaps will help somebody.

    Keep in mind, you have to specify which categories you want to make available within the code, if this were made into a plugin there are much better ways of doing it.

    add_filter(‘attachment_fields_to_edit’, ‘ms_attachment_fields_to_edit’, 11, 2);
    add_filter(‘attachment_fields_to_save’, ‘ms_image_attachment_fields_to_save’, 11, 2);

    function ms_attachment_fields_to_edit($form_fields, $post) {

    // Define categories we want to use
    $category_array[“gallery_block_1″]=”Front Page Gallery Block 1”;
    $category_array[“gallery_block_2″]=”Front Page Gallery Block 2”;
    $category_array[“gallery_block_3″]=”Front Page Gallery Block 3”;
    $category_array[“gallery_block_tall”]=”Front Page Gallery Tall Block”;

    $post_id = $_GET[‘attachment_id’];
    $meta = wp_get_attachment_metadata( $post_id );
    $image_gallery_folder = $meta[‘image_meta’][‘image_gallery_folder’];

    $draw_gallery_folder_dropdown = “<select name=’image_gallery_folder’ id=’image_gallery_folder’>”;

    foreach ($category_array as $key => $value) {
    $draw_gallery_folder_dropdown .= “<option “;
    if($image_gallery_folder == $key) $draw_gallery_folder_dropdown .= ” SELECTED “;
    $draw_gallery_folder_dropdown .= “value=’$key’>$value</option>”;
    }
    $draw_gallery_folder_dropdown .=”</select>”;

    if ( substr($post->post_mime_type, 0, 5) == ‘image’ ) {
    $form_fields[‘image_gallery_folder’] = array(
    ‘label’ => __(‘Image Folder’),
    ‘input’ => ‘html’,
    ‘html’ => $draw_gallery_folder_dropdown
    );}

    return $form_fields;
    }

    function ms_image_attachment_fields_to_save($post, $attachment) {

    if ( substr($post[‘post_mime_type’], 0, 5) == ‘image’ ) {
    $attachment_id = $post[‘ID’];
    $meta = wp_get_attachment_metadata( $attachment_id );
    $meta[‘image_meta’][‘image_gallery_folder’] = $_POST[‘image_gallery_folder’];
    wp_update_attachment_metadata( $attachment_id, $meta );
    }
    return $post;
    }

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘attachment fields as image meta data?’ is closed to new replies.