Last active
December 22, 2021 21:02
-
-
Save chrono-meter/8e92ba78b7f146629f74847406e9c91c to your computer and use it in GitHub Desktop.
CMB2 option example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: CMB2 option test | |
* Author: chrono-meter@gmx.net | |
* Version: 20211223 | |
* | |
* @link https://github.com/CMB2/CMB2-Snippet-Library/search?q=object_types&unscoped_q=object_types | |
* @see add_menu_page() | |
* | |
* to get variables: cmb2_get_option('myprefix_network_options', 'key', 'default') | |
*/ | |
add_action( 'cmb2_admin_init', function () { | |
$cmb_options = new_cmb2_box( [ | |
'id' => 'myprefix_network_option_metabox', | |
'object_types' => [ 'options-page' ], | |
// admin_menu|network_admin_menu | |
'admin_menu_hook' => 'admin_menu', | |
/** @link https://codex.wordpress.org/Adding_Administration_Menus#Using_add_submenu_page */ | |
'parent_slug' => 'options-general.php', | |
'title' => esc_html__( 'My options', 'myprefix' ), | |
// 'menu_title' => '', // menu title (default: 'title' property's value) | |
'capability' => 'manage_options', | |
'option_key' => 'myprefix_network_options', // 1st arg for cmb2_get_option | |
// 'icon_url' => 'dashicons-palmtree', | |
// 'position' => 1, | |
/** @link https://github.com/CMB2/CMB2-Snippet-Library/blob/master/options-and-settings-pages/custom-display-callback.php */ | |
// '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'. | |
] ); | |
$cmb_options->add_field( [ | |
'name' => __( 'Test Text', 'myprefix' ), | |
'desc' => __( 'field description (optional)', 'myprefix' ), | |
'id' => 'test_text', | |
'type' => 'text', | |
'default' => 'Default Text', | |
] ); | |
$cmb_options->add_field( [ | |
'name' => __( 'Test Color Picker', 'myprefix' ), | |
'desc' => __( 'field description (optional)', 'myprefix' ), | |
'id' => 'test_colorpicker', | |
'type' => 'colorpicker', | |
'default' => '#bada55', | |
] ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment