WordPress附件分类复选框组未在网格视图中保存

Jam*_*mes 4 javascript php wordpress admin-ajax

我创建了一个分类法并将其附加到附件或媒体上.

我正在使用"高级自定义字段"添加分类字段,该字段显示为复选框组.它将字段正确添加到网格和列表视图中的附件页面.

在列表视图中,字段正确更新,但在网格视图中,它仅保存单击的最后一个复选框.这是由某种onclick函数引起的,当单击每个复选框时会激活admin-ajax.因此,它只发送单击框的值.我试图找到一种方法来改变js函数以添加复选框组和多选.

更新:我已将该功能跟踪到一个onchange.

功能:保存

file:media-views.min.js

用于复制问题的代码:

分类:

function cptui_register_my_taxes() {
    $labels = array(
        "name" => __( "Image Tags", "" ),
        "singular_name" => __( "Image Tag", "" ),
    );
    $args = array(
        "label" => __( "Image Tags", "" ),
        "labels" => $labels,
        "public" => true,
        "hierarchical" => false,
        "label" => "Image Tags",
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => false,
        "query_var" => true,
        "rewrite" => false,
        "show_admin_column" => false,
        "show_in_rest" => false,
        "rest_base" => "imagetags",
        "show_in_quick_edit" => false,
    );
    register_taxonomy( "imagetags", array( "attachment" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes' );
Run Code Online (Sandbox Code Playgroud)

自定义字段:

if( function_exists('acf_add_local_field_group') ):
    acf_add_local_field_group(array(
        'key' => 'group_5bc3f242c39e3',
        'title' => 'Gallery - Image Tags',
        'fields' => array(
            array(
                'key' => 'field_5bc3f249f009c',
                'label' => 'Image Tags',
                'name' => 'new_image_tags',
                'type' => 'taxonomy',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'taxonomy' => 'imagetags',
                'field_type' => 'checkbox',
                'add_term' => 1,
                'save_terms' => 1,
                'load_terms' => 1,
                'return_format' => 'id',
                'multiple' => 0,
                'allow_null' => 0,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'attachment',
                    'operator' => '==',
                    'value' => 'all',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => 1,
        'description' => '',
    ));

endif;
Run Code Online (Sandbox Code Playgroud)

如果需要,还可以使用以下代码删除默认分类法字段.它不会影响代码,但会删除文本字段:

// Remove taxonomy from the attachment pages so the acf taxonomy can work
add_action( 'admin_menu', function (){ remove_meta_box('imagetagsdiv', 'attachment', 'side' ); } );

// Add this in to remove it from the popup editor
add_filter( 'attachment_fields_to_edit', function( $fields ){
    unset($fields['imagetags']); 
    return $fields;
} );
Run Code Online (Sandbox Code Playgroud)

发现问题现在找到解决方案

打开wp-includes/js/media-views.js - 行:8353

_.each( this.$el.serializeArray(), function( pair ) {
    data[ pair.name ] = pair.value;
});
Run Code Online (Sandbox Code Playgroud)

它似乎使用此代码来防止重复,但实际上它正在删除可以作为数组提交的所有内容.

注意:这是完整文件WP默认加载.min版本

Sal*_* CJ 5

可以通过覆盖原型函数(wp.media.view.AttachmentCompat.prototype.save())来解决这个问题,如下所示:

add_action( 'admin_print_footer_scripts', 'so52810006', 11 );
function so52810006() {
    // Make sure the media-views script has been enqueued.
    if ( ! did_action( 'wp_enqueue_media' ) ) {
        return;
    }
    ?>
<script>
wp.media.view.AttachmentCompat.prototype.save = function( event ) {
    var data = {};

    if ( event ) {
        event.preventDefault();
    }

    _.each( this.$el.serializeArray(), function( pair ) {
        if ( /\[\]$/.test( pair.name ) ) {
            if ( undefined === data[ pair.name ] ) {
                data[ pair.name ] = [];
            }
            data[ pair.name ].push( pair.value );
        } else {
            data[ pair.name ] = pair.value;
        }
    });

    this.controller.trigger( 'attachment:compat:waiting', ['waiting'] );
    this.model.saveCompat( data ).always( _.bind( this.postSave, this ) );
};
</script>
    <?php
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,主要部分在这里,它data通过考虑字段名称来正确构建,pair.name即它是否以[]指示数组值的结尾.

_.each( this.$el.serializeArray(), function( pair ) {
    if ( /\[\]$/.test( pair.name ) ) {
        if ( undefined === data[ pair.name ] ) {
            data[ pair.name ] = [];
        }
        data[ pair.name ].push( pair.value );
    } else {
        data[ pair.name ] = pair.value;
    }
});
Run Code Online (Sandbox Code Playgroud)

尝试使用WordPress 4.9.8和ACF 5.7.7进行测试和测试.