CMB2有一个选项可用作选项页面。
我在示例文件中查找,并在wiki页面上查找,但即使将示例复制并粘贴到文件上也不起作用。
我可能遗漏了一些东西,但我找不到它是什么,我已经花了两天的时间尝试让它工作。
按照我修改为以下代码的wiki和示例
add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
function yourprefix_register_theme_options_metabox() {
$option_key = 'wherever';
$cmb = new_cmb2_box( array(
'id'=> $option_key . '_theme_options-page',
'object_types' => array( 'options-page' ),
'hookup' => false,
'menu_title' => 'Site Options',
'parent_slug' => 'tools.php',
'capability' => 'manage_options'
) );
$cmb->add_field( array(
'name' => 'Site Background Color',
'desc' => 'field description',
'id' => 'bg_color',
'type' => 'colorpicker',
'default' => '#ffffff'
) );
}有什么线索可以解释为什么它不起作用吗?
发布于 2019-01-25 15:32:09
目前关于CMB2的选项页面功能的文档只是将你带到他们的Snippet Library,这并不是100%直接的,所以希望我能帮助你阐明如何正确使用这些功能。
首先,您在cmb2_admin_init中注册的metaboxes可以生成整个管理页面。例如,直接从代码片段库中获取以下代码示例:
add_action('cmb2_admin_init', 'register_my_admin_page');
function register_my_admin_page() {
/**
* Registers options page menu item and form.
*/
$cmb_options = new_cmb2_box( array(
'id' => 'myprefix_option_metabox',
'title' => esc_html__( 'Site Options', 'myprefix' ),
'object_types' => array( 'options-page' ),
/*
* The following parameters are specific to the options-page box
* Several of these parameters are passed along to add_menu_page()/add_submenu_page().
*/
'option_key' => 'myprefix_options', // The option key and admin menu page slug.
// 'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
// 'menu_title' => esc_html__( 'Options', 'myprefix' ), // Falls back to 'title' (above).
// 'parent_slug' => 'themes.php', // Make options page a submenu item of the themes menu.
// 'capability' => 'manage_options', // Cap required to view options-page.
// 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
// 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
// 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
// 'save_button' => esc_html__( 'Save Theme Options', 'myprefix' ), // The text for the options-page save button. Defaults to 'Save'.
) );
/*
* Options fields ids only need
* to be unique within this box.
* Prefix is not needed.
*/
$cmb_options->add_field( array(
'name' => __( 'Test Text', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_text',
'type' => 'text',
'default' => 'Default Text',
) );
$cmb_options->add_field( array(
'name' => __( 'Test Color Picker', 'myprefix' ),
'desc' => __( 'field description (optional)', 'myprefix' ),
'id' => 'test_colorpicker',
'type' => 'colorpicker',
'default' => '#bada55',
) );
}这段代码将生成一个名为"Site Options“的顶级管理页面,其中包含两个字段:文本字段和颜色选择器字段,以及标题、表单域、提交按钮等。您可以使用new_cmb2_box函数上的注释掉设置来配置页面向用户显示的方式。
保存表单时,它会将元框及其字段保存到站点选项myprefix_options。因此,如果调用函数get_option('myprefix_options'),它将返回以下数组:
array(
'myprefix_option_metabox' => array(
'test_text' => '' // value of the Test Text field,
'test_colorpicker' => '' // value of the Test Color Picker field
)
)我希望这有助于澄清一些事情。
https://stackoverflow.com/questions/44082191
复制相似问题