Add fields to the WordPress media uploader

I am trying to add an URL field to the WordPress media uploader using following code:

/**
 * Add an URL field to gallery attachments
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */

function add_attachment_url_field( $form_fields, $post ) {
    $form_fields['partner_url'] = array(
        'label' => 'Link',
        'input' => 'url',
        'value' => get_post_meta( $post->ID, 'partner_url', true ),
        'helps' => '',
    );

    return $form_fields;
}

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

/**
 * Save values of 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 save_attachment_url_field( $post, $attachment ) {
    if( isset( $attachment['partner_url'] ) )
        update_post_meta( $post['ID'], 'partner_url', esc_url( $attachment['partner_url'] ) );

    return $post;
}

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

This adds the field, but does not save its value after I click update. How to get it to work?

0

Leave a Comment