and my final code is below, issue is that if i enter some data and save then it is not saving. Form is also generated properly .. See images below.. Please tell me where i am doing wrong. waiting for reply.
<?php
/**
* CMB2 Theme Options @version 0.1.0
*/ class Euronet_Admin {
/**
* Option key, and option page slug
* @var string
*/
private $key = 'euronet_options';
/**
* Options page metabox id
* @var string
*/
private $metabox_id = 'euronet_option_metabox';
/**
* Options Page title
* @var string
*/
protected $title = '';
/**
* Options Page hook
* @var string
*/
protected $options_page = '';
/**
* Holds an instance of the object
*
* @var Myprefix_Admin
**/
private static $instance = null;
/**
* Constructor
* @since 0.1.0
*/
private function __construct() {
// Set our title
$this->title = __( 'Theme Options', 'euronet' );
}
/**CMB array **/
protected $cmb=array();
/**
* Returns the running object
*
* @return Myprefix_Admin
**/
public static function get_instance() {
if( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Initiate our hooks
* @since 0.1.0
*/
public function hooks() {
add_action( 'admin_init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
add_action( 'cmb2_admin_init', array( $this, 'add_options_page_metabox' ) );
// add_action( 'cmb2_admin_init', array( $this, 'add_counters_metabox' ) );
}
/**
* Register our setting to WP
* @since 0.1.0
*/
public function init() {
register_setting( $this->key, $this->key );
}
/**
* Add menu options page
* @since 0.1.0
*/
public function add_options_page() {
$this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', $this->key, array( $this, 'admin_page_display' ) );
// Include CMB CSS in the head to avoid FOUC
add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
}
/**
* Admin page markup. Mostly handled by CMB2
* @since 0.1.0
*/
public function admin_page_display() {
$option_tabs = self::add_options_page_metabox(); //get all option tabs
$tab_forms = array();
echo '<pre>';
// print_r($option_tabs);
echo '</pre>';
?>
<link rel='stylesheet' id='theme_options-css' href='<?php echo get_stylesheet_directory_uri(); ?>/css/theme_options.css' type='text/css' media='all' />
<div class="wrap cmb2-options-page <?php echo $this->key; ?>">
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<!--Accordions-->
<?php foreach($option_tabs as $option_tab) { ?>
<?php
echo '<pre>';
print_r($option_tab);
echo '</pre>';
?>
<button class="accordion"><?php echo $option_tab->meta_box['title']; ?></button>
<div class="panel">
<?php cmb2_metabox_form( $option_tab->meta_box['id'], $option_tab->meta_box['key'] ); ?>
<div class="clear"></div>
</div>
<?php } ?>
<!--Accordions-->
<script type="text/javascript">
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
</script>
</div>
<?php
}
/**
* Add the options metabox to the array of metaboxes
* @since 0.1.0
*/
function add_options_page_metabox() {
// Only need to initiate the array once per page-load
if ( ! empty( $this->option_metabox ) ) {
return $this->option_metabox;
}
// hook in our save notices
add_action( "cmb2_save_options-page_fields_{$this->metabox_id}", array( $this, 'settings_notices' ), 10, 2 );
$this->cmb[0] = new_cmb2_box( array(
'id' => $this->metabox_id,
'title'=> 'Language Flags',
'hookup' => false,
'cmb_styles' => false,
'show_on' => array(
// These are important, don't remove
'key' => 'options-page',
'value' => array( $this->key, )
),
) );
// Set our CMB2 fields
$group_field_id = $this->cmb[0]->add_field( array(
'id' => 'wiki_test_repeat_group',
'type' => 'group',
'description' => __( 'Generates reusable form entries', 'cmb2' ),
// 'repeatable' => false, // use false if you want non-repeatable group
'options' => array(
'group_title' => __( 'Language {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number
'add_button' => __( 'Add Another Language', 'cmb2' ),
'remove_button' => __( 'Remove Language', 'cmb2' ),
'sortable' => true, // beta
'closed' => true, // true to have the groups closed by default
),
) );
// Id's for group's fields only need to be unique for the group. Prefix is not needed.
$this->cmb[0]->add_group_field( $group_field_id, array(
'name' => 'Language Title',
'id' => 'title',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$this->cmb[0]->add_group_field( $group_field_id, array(
'name' => 'Language Url',
'id' => 'url',
'type' => 'text'
) );
$this->cmb[0]->add_group_field( $group_field_id, array(
'name' => 'Language Flag',
'id' => 'image',
'type' => 'file',
) );
$this->cmb[1] = new_cmb2_box( array(
'id' => 'number_counters',
'title' =>'Number Counters',
'hookup' => false,
'cmb_styles' => false,
'show_on' => array(
// These are important, don't remove
'key' => 'options-page',
'value' => array( 'num_counters_mb', )
),
) );
$this->cmb[1]->add_field( array(
'name' => 'Test Text',
'desc' => 'field description (optional)',
'default' => 'standard value (optional)',
'id' => 'wiki_test_text',
'type' => 'text',
) );
return $this->cmb;
}
/**
* Register settings notices for display
*
* @since 0.1.0
* @param int $object_id Option key
* @param array $updated Array of updated fields
* @return void
*/
public function settings_notices( $object_id, $updated ) {
if ( $object_id !== $this->key || empty( $updated ) ) {
return;
}
add_settings_error( $this->key . '-notices', '', __( 'Settings updated.', 'euronet' ), 'updated' );
settings_errors( $this->key . '-notices' );
}
/**
* Public getter method for retrieving protected/private variables
* @since 0.1.0
* @param string $field Field to retrieve
* @return mixed Field value or exception is thrown
*/
public function __get( $field ) {
// Allowed fields to retrieve
if ( in_array( $field, array( 'key', 'metabox_id', 'title', 'options_page' ), true ) ) {
return $this->{$field};
}
throw new Exception( 'Invalid property: ' . $field );
}
}
/**
* Helper function to get/return the Myprefix_Admin object
* @since 0.1.0
* @return Myprefix_Admin object
*/
function euronet_admin() {
return Euronet_Admin::get_instance();
}
/**
* Wrapper function around cmb2_get_option
* @since 0.1.0
* @param string $key Options array key
* @return mixed Option value
*/
function euronet_get_option( $key = '' ) {
return cmb2_get_option( euronet_admin()->key, $key, true );
}
// Get it started
euronet_admin();
another thing is that i am showing both metabox objects at one page.