• Hello

    I’ve added two custom fields, ‘Publisher Name’ and ‘Publication Date’ to allow me to add information to files in the Media Library (fields accessible through the Edit Media page). This has been done using the ‘attachment fields to edit / attachment fields to save’ method as below:

    /**
    * Add Publisher Name and Publication Date fields to media uploader
    *
    * @param $form_fields array, fields to include in attachment form
    * @param $post object, attachment record in database
    * @return $form_fields, modified form fields
    */

    function be_attachment_field_credit( $form_fields, $post ) {
    $form_fields[‘be-publisher-name’] = array(
    ‘label’ => ‘Publisher Name’,
    ‘input’ => ‘text’,
    ‘value’ => get_post_meta( $post->ID, ‘be_publisher_name’, true ),
    ‘helps’ => ‘If provided, the publisher name will be displayed in the report homepage’,
    );

    $form_fields[‘be-publication-date’] = array(
    ‘label’ => ‘Publication Date’,
    ‘input’ => ‘date’,
    ‘value’ => get_post_meta( $post->ID, ‘be_publication_date’, true ),
    ‘helps’ => ‘Add Publication Date’,
    );

    return $form_fields;
    }

    add_filter( ‘attachment_fields_to_edit’, ‘be_attachment_field_credit’, 10, 2 );

    /**
    * Save values of Publisher Name and Publication Date 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 be_attachment_field_credit_save( $post, $attachment ) {
    if( isset( $attachment[‘be-publisher-name’] ) )
    update_post_meta( $post[‘ID’], ‘be_publisher_name’, $attachment[‘be-publisher-name’] );

    if( isset( $attachment[‘be-publication-date’] ) )
    update_post_meta( $post[‘ID’], ‘be_publication_date’, $attachment[‘be-publication-date’] );

    return $post;
    }

    add_filter( ‘attachment_fields_to_save’, ‘be_attachment_field_credit_save’, 10, 2 );

    My question is how can I add the Publication Date field as an actual Date (preferably as a date picker) rather than plain text, which is how it is recognised using the above code? I tried to specify the ‘input’ field as ‘date’, but this does not work. Any ideas?

    Thanks in advance

    Andy

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Adding a custom date field to Media Library Files’ is closed to new replies.