[英]CMB2 - how to add metabox to an existing options page (options page created also by new_cmb2_box)?
What do I want to accomplish ?我想完成什么?
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'
) );
}
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.