Skip to main content
added 42 characters in body
Source Link
BlueHelmet
  • 179
  • 1
  • 10

I have a working function that adds a meta field to all attachments (so I can enter the url of a video). I would like this field to populate only on attachments in the "video" category.

I've tried the below, by adding in_category('video') but that didn't work. The field still shows on all attachments.

function attachment_field_url( $form_fields, $post ) {
    
        $form_fields['video-url'] = array(
        'label' => 'Video URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'video_url', true ),
        'helps' => 'Add video URL',
    );

    return $form_fields;
}

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

function attachment_field_url_save( $post, $attachment ) {
    
    if( in_category('video')  && !isset ($attachment['video-url'] ) )
update_post_meta( $post['ID'], 'video_url', esc_url( $attachment['video-url'] ) );                             
    return $post;
}

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

I have a working function that adds a meta field to all attachments (so I can enter the url of a video). I would like this field to populate only on attachments in the "video" category.

I've tried the below, by adding in_category('video') but that didn't work.

function attachment_field_url( $form_fields, $post ) {
    
        $form_fields['video-url'] = array(
        'label' => 'Video URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'video_url', true ),
        'helps' => 'Add video URL',
    );

    return $form_fields;
}

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

function attachment_field_url_save( $post, $attachment ) {
    
    if( in_category('video')  && !isset ($attachment['video-url'] ) )
update_post_meta( $post['ID'], 'video_url', esc_url( $attachment['video-url'] ) );                             
    return $post;
}

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

I have a working function that adds a meta field to all attachments (so I can enter the url of a video). I would like this field to populate only on attachments in the "video" category.

I've tried the below, by adding in_category('video') but that didn't work. The field still shows on all attachments.

function attachment_field_url( $form_fields, $post ) {
    
        $form_fields['video-url'] = array(
        'label' => 'Video URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'video_url', true ),
        'helps' => 'Add video URL',
    );

    return $form_fields;
}

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

function attachment_field_url_save( $post, $attachment ) {
    
    if( in_category('video')  && !isset ($attachment['video-url'] ) )
update_post_meta( $post['ID'], 'video_url', esc_url( $attachment['video-url'] ) );                             
    return $post;
}

add_filter( 'attachment_fields_to_save', 'attachment_field_url_save', 10, 2 );
Source Link
BlueHelmet
  • 179
  • 1
  • 10

Add custom field to attachments in a specific category

I have a working function that adds a meta field to all attachments (so I can enter the url of a video). I would like this field to populate only on attachments in the "video" category.

I've tried the below, by adding in_category('video') but that didn't work.

function attachment_field_url( $form_fields, $post ) {
    
        $form_fields['video-url'] = array(
        'label' => 'Video URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'video_url', true ),
        'helps' => 'Add video URL',
    );

    return $form_fields;
}

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

function attachment_field_url_save( $post, $attachment ) {
    
    if( in_category('video')  && !isset ($attachment['video-url'] ) )
update_post_meta( $post['ID'], 'video_url', esc_url( $attachment['video-url'] ) );                             
    return $post;
}

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