简体   繁体   English

CMB2 - 如何将 metabox 添加到现有选项页面(也由 new_cmb2_box 创建的选项页面)?

[英]CMB2 - how to add metabox to an existing options page (options page created also by new_cmb2_box)?

What do I want to accomplish ?我想完成什么?

  1. Add new options-page with the use of new_cmb2_box -> this works OK使用 new_cmb2_box 添加新的选项页面 ->这可以正常工作

add_action( 'cmb2_admin_init', 'register_metabox' );

function register_metabox() {

    $cmb = new_cmb2_box( array(
        'id'           => 'testtesttest',
        'title'         => '_Ustawienia_Motywu_',
        'object_types' => array( 'options-page' ),
        'option_key'      => 'ustawienia_motywu',
        'parent_slug' => 'options-general.php'
    ) );

    }

  1. To just created options page (point 1 above) add one meta box, again, with the use of new_cmb2_box -> THIS PART DOES NOT WORK - how to make it work ?对于刚刚创建的选项页面(上面的第 1 点),再次添加一个元框,使用 new_cmb2_box ->这部分不起作用 - 如何使它起作用? :

add_action( 'cmb2_admin_init', 'register_metabox2' );

function register_metabox2() {

    $cmb2 = new_cmb2_box( array(
        'id'           => 'test2',
        'title'         => 'title',
        'object_types' => array( 'options-page' ),
        'parent_slug' => 'options-general.php?page=ustawienia_motywu',
    ) );

    // Set our CMB2 fields

    $cmb2->add_field( array(
        'name' => __( 'Test Text', 'myprefix' ),
        'desc' => __( 'field description (optional)', 'myprefix' ),
        'id'   => 'test_text',
        'type' => 'text',
        // 'default' => 'Default Text',
    ) );

    $cmb2->add_field( array(
        'name'    => __( 'Test Color Picker', 'myprefix' ),
        'desc'    => __( 'field description (optional)', 'myprefix' ),
        'id'      => 'test_colorpicker',
        'type'    => 'colorpicker',
        'default' => '#bada55',
    ) );

}

How to make #2 work ?如何使#2 工作? My guess is maby to use custom 'show_on_cb' ...我的猜测是 maby 使用自定义 'show_on_cb' ...

You should put all to one function:您应该将所有内容都放在一个函数中:

add_action( 'cmb2_admin_init', 'register_metabox' );
function register_metabox() {

    $cmb = new_cmb2_box( array(
        'id'            => 'testtesttest',
        'title'         => '_Ustawienia_Motywu_',
        'object_types'  => array( 'options-page' ),
        'option_key'    => 'ustawienia_motywu',
        'parent_slug'   => 'options-general.php'
    ) );

    $cmb->add_field( array(
        'name' => __( 'Test Text', 'myprefix' ),
        'desc' => __( 'field description (optional)', 'myprefix' ),
        'id'   => 'test_text',
        'type' => 'text',
    ) );

    $cmb->add_field( array(
        'name'    => __( 'Test Color Picker', 'myprefix' ),
        'desc'    => __( 'field description (optional)', 'myprefix' ),
        'id'      => 'test_colorpicker',
        'type'    => 'colorpicker',
        'default' => '#bada55',
    ) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM