attachments - Custom field in media library not saving, selected() function not adding "selected" to select li

Following the approach in the developer docs for selected() but can't get it to work with a custom field in the med

Following the approach in the developer docs for selected() but can't get it to work with a custom field in the media library for a select list.

It won't save the value, or become "selected". Looks like WP is doing some js/ajax in the background here, so do I need to approach this differently or have a dumb typo?

Please see code below, help is appreciated.

/*
Add license field to media attachments 
*/

    function add_custom_field_license( $form_fields, $post ) {
        $license_field = get_post_meta($post->ID, 'license_field');
        $form_fields['license_field'] = [
            'label' => 'License',
            'input' => 'html',
            'html' => "<select name='attachments[{$post->ID}][license_field]' id='attachments-{$post->ID}-license_field'> 
        <option value='none' " . selected($license_field, "none") . ">None (all rights reserved)</option>
        <option value='CC0' " . selected($license_field, "CC0") . ">CC0</option>
        <option value='CC BY' " . selected($license_field, "CC BY") . ">CC BY</option>
        </select>",
        ];
        return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'add_custom_field_license', null, 2); 



/*
Save license field to media attachments
*/
function save_custom_field_license($post, $attachment) {  
    if( isset($attachment['license_field']) ){  
        update_post_meta($post['ID'], 'license_field', sanitize_text_field( $attachment['license_field'] ) );  
    }else{
         delete_post_meta($post['ID'], 'license_field' );
    }
    return $post;  
}
add_filter('attachment_fields_to_save', 'save_custom_field_licenser', null, 2);

Following the approach in the developer docs for selected() but can't get it to work with a custom field in the media library for a select list.

It won't save the value, or become "selected". Looks like WP is doing some js/ajax in the background here, so do I need to approach this differently or have a dumb typo?

Please see code below, help is appreciated.

/*
Add license field to media attachments 
*/

    function add_custom_field_license( $form_fields, $post ) {
        $license_field = get_post_meta($post->ID, 'license_field');
        $form_fields['license_field'] = [
            'label' => 'License',
            'input' => 'html',
            'html' => "<select name='attachments[{$post->ID}][license_field]' id='attachments-{$post->ID}-license_field'> 
        <option value='none' " . selected($license_field, "none") . ">None (all rights reserved)</option>
        <option value='CC0' " . selected($license_field, "CC0") . ">CC0</option>
        <option value='CC BY' " . selected($license_field, "CC BY") . ">CC BY</option>
        </select>",
        ];
        return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'add_custom_field_license', null, 2); 



/*
Save license field to media attachments
*/
function save_custom_field_license($post, $attachment) {  
    if( isset($attachment['license_field']) ){  
        update_post_meta($post['ID'], 'license_field', sanitize_text_field( $attachment['license_field'] ) );  
    }else{
         delete_post_meta($post['ID'], 'license_field' );
    }
    return $post;  
}
add_filter('attachment_fields_to_save', 'save_custom_field_licenser', null, 2);
Share Improve this question asked Jul 10, 2020 at 0:07 OneFishTacoOneFishTaco 1013 bronze badges 4
  • 1 you've got an extra 'r' there on save_custom_field_licenser in the second add_filter() – mozboz Commented Jul 10, 2020 at 0:37
  • And it would make sense that if it doesn't save then it doesn't have a value in the db from which to render the select, unless you manually inserted it somehow, so maybe selected() will work if that was all it was – mozboz Commented Jul 10, 2020 at 0:39
  • 1 replace the define to thisadd_filter('attachment_fields_to_save', 'save_custom_field_license', null, 2); – RachC Commented Jul 10, 2020 at 0:47
  • 1 In addition to correcting the typo ("licenser"), you should set the 3rd parameter for get_post_meta() to true (to get a single value), and the 3rd parameter for selected() to false (to not echo the attribute). – Sally CJ Commented Jul 10, 2020 at 1:55
Add a comment  | 

1 Answer 1

Reset to default 0

As per outlined in the comments, by fixing a typo in my save function, and setting the third parameter in get_post_meta() to true, and false in selected() it worked liked a charm. Thanks everyone!

Here's the working code in case anyone is trying to add a select list custom field to their media manager:

   /*
    Add license field to media attachments 
    */
    
    function add_custom_field_license( $form_fields, $post ) {
        $license_field = get_post_meta($post->ID, 'license_field', true);
        $form_fields['license_field'] = [
            'label' => 'License',
            'input' => 'html',
            'html' => "<select name='attachments[{$post->ID}][license_field]' id='attachments-{$post->ID}-license_field'> 
        <option value='none' " . selected($license_field, "none", false) . ">None (all rights reserved)</option>
        <option value='CC0' " . selected($license_field, "CC0", false) . ">CC0</option>
        <option value='CC BY' " . selected($license_field, "CC BY", false) . ">CC BY</option>
        <option value='CC BY-NC' " . selected($license_field, "CC BY-NC", false) . ">CC BY-NC</option>
        <option value='CC BY-SA' " . selected($license_field, "CC BY-SA", false) . ">CC BY-SA</option>
        <option value='CC BY-NC-ND' " . selected($license_field, "CC BY-NC-ND", false) . ">CC BY-NC-ND</option>
        </select>",
        ];
        return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'add_custom_field_license', null, 2); 
     
    /*
    Save license field to media attachments
    */

    function save_custom_field_license($post, $attachment) {  
        if( isset($attachment['license_field']) ){  
            update_post_meta($post['ID'], 'license_field', sanitize_text_field( $attachment['license_field'] ) );  
        }else{
             delete_post_meta($post['ID'], 'license_field' );
        }
        return $post;  
    }
    add_filter('attachment_fields_to_save', 'save_custom_field_license', null, 2);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742277154a4413805.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信