未保存媒体库中的自定义字段,SELECTED()函数未将“SELECTED”添加到选择列表输入类型

时间:2020-07-10 作者:AlexP

遵循开发人员文档中的方法selected() 但无法使其与媒体库中的自定义字段一起用于选择列表。

它不会保存价值,也不会变成;已选定;。看起来WP正在后台执行一些js/ajax,所以我需要以不同的方式来处理这个问题,还是有一个愚蠢的拼写错误?

请参阅下面的代码,非常感谢您的帮助。

/*
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);

1 个回复
最合适的回答,由SO网友:AlexP 整理而成

根据注释中的概述,通过在我的保存功能中修复一个输入错误,并在中设置第三个参数get_post_meta() 为真,为假selected() 它很有魅力。谢谢大家!

如果有人试图将“选择列表”自定义字段添加到他们的媒体管理器中,以下是工作代码:

   /*
    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);

相关推荐

delete post also attachments

我正在尝试删除包含所有附件的帖子。这就是我现在想到的函数;function remove_post(){ if(isset($_POST[\'post_id\']) && is_numeric($_POST[\'post_id\'])){ $post = get_post($_POST[\'post_id\']); if(get_current_user_id() == $pos