• Resolved bucklea

    (@bucklea)


    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 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The function that uses the ‘attachment fields to edit’ filter only accepts certain input types, much fewer than those available to html5. As date is not one of them, you will have to accept the plain text at this point.

    I was hoping you could change the input type later at a different filter, but after a cursory check, I didn’t see any. I could have easily missed one though, so a more determined search may be warranted.

    If all else fails, you could change the field type at the client with jQuery. Your save script should be ready to deal with either input just in case jQuery fails to change the field.

    Thread Starter bucklea

    (@bucklea)

    Hi bcworkz

    Thank you so much for your response and suggestion.

    Unfortunately I don’t know how to change the field type at the client with jQuery, it’s beyond my understanding.

    I’ve conceded defeat on this one and used the “Types” plugin to implement custom fields, which seems to work so far!

    Andy

    Could you elaborate on how you did it with “Types” plugin?

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