CMB2 page options parameter

CMB2 has the ability to be used as a parameter page.

I go through the example files and on the wiki page, but even copying and pasting the example in the files it doesn't work.

I probably missed something, but can't find what it is, I've been trying to make this work for two days now.

Following wiki and example, I changed this code

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'
    ) );

}

      

Any leads to why it doesn't work?

+3


source to share


1 answer


Currently, the documentation for the CMB2 options page capabilities just takes you to their Snippet library, which is not 100% straightforward, so hopefully I can help clarify how to use these features correctly.

First, the meta boxes you register with cmb2_admin_init

can generate the entire admin page. Take this sample code straight from the snippets library, for example:

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',
  ) );
}

      

This code snippet will generate a top-level admin page named "Site Settings" with two fields: a text field and a color picker, with a title, form fields, a submit button, and so on. You can customize how the page is displayed. displayed to the user using the commented out function settings new_cmb2_box

.



After saving the form, the meta field and its fields will be saved in the site parameter myprefix_options

. Therefore, if you are a get_option('myprefix_options')

function get_option('myprefix_options')

, it will return the following array:

array(
  'myprefix_option_metabox' => array(
    'test_text' => '' // value of the Test Text field,
    'test_colorpicker' => '' // value of the Test Color Picker field
  )
)

      

Hope this helps clarify things a bit.

0


source







All Articles