WordPress - Hidden custom meta field for images

I'm trying to add a custom field to the media uploader in WordPress. I ahve it working, but I'd like to make the custom field key a hidden one.

If you are familiar with the way WordPress handles custom fields, you'll know that setting the key to "_something" will hide that key from the drop down lists visible to the user.

/**
 * Add Video URL fields to media uploader
 *
 * http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */

    function capgun2012_attachment_fields( $form_fields, $post ) {

        $form_fields['capgun2012_video_url'] = array(
            'label' => 'Vimeo URL',
            'input' => 'text',
            'value' => get_post_meta( $post->ID, 'capgun2012_video_url', true ),
            'helps' => 'If provided, photo will be displayed as a video',
        );

        return $form_fields;
    }

    add_filter( 'attachment_fields_to_edit', 'capgun2012_attachment_fields', 10, 2 );

/**
 * Save values of Photographer Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

    function capgun2012_attachment_fields_save( $post, $attachment ) {

        if( isset( $attachment['capgun2012_video_url'] ) ) {
            update_post_meta( $post['ID'], 'capgun2012_video_url', $attachment['capgun2012_video_url'] );
        }

        return $post;
    }

    add_filter( 'attachment_fields_to_save', 'capgun2012_attachment_fields_save', 10, 2 );  

If I simply replace all the occurrences of "capgun2012_video_url" with "_capgun2012_video_url" then it doesn't work. I'm starting to think that the media uploader doesn't play well with hidden custom fields.

Please see attached screenshot of what I don't want to happen (the custom key showing in the custom fields drop down).

Thanks for the help.

Answers


This was answered here: http://devilsworkshop.org/adding-custom-fields-to-wordpress-media-gallery-upload/

Setup field:

   /* For adding custom field to gallery popup */
function rt_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["rt-image-link"] = array(
        "label" => __("Custom Link"),
        "input" => "text", // this is default if "input" is omitted
        "value" => get_post_meta($post->ID, "_rt-image-link", true),
                "helps" => __("To be used with special slider added via [rt_carousel] shortcode."),
    );
   return $form_fields;
}
// now attach our function to the hook
add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2);

And this to save it:

function rt_image_attachment_fields_to_save($post, $attachment) {
    // $attachment part of the form $_POST ($_POST[attachments][postID])
        // $post['post_type'] == 'attachment'
    if( isset($attachment['rt-image-link']) ){
        // update_post_meta(postID, meta_key, meta_value);
        update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
    }
    return $post;
}
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);

Need Your Help

How to use the Rhino javascript engine in an applet

java security applet rhino accesscontrolexception

For my java program I'm using Rhino to execute JS scripts. Now I'm trying to convert it to an applet which works great, except that everytime it's calling evaluateString(...) the JVM throws an

Ubuntu file ownership cpp

c++ file ubuntu ownership

As in description. I need to find out who own a file using cpp code, i am using boost filesystem, codeblocks on ubuntu 12.04 (which is irrelevant, but). If anyone could give me function name, or