• If I use a repeatable group on the “options-page” using cmb2_metabox_form, it is able to display/add/remove the groups but on saving it only saves the first group.

    I’ve checked the $_POST data in the method cmb2_print_metabox_form in helper-functions.php method and it contains only the first group.

    register cmb2 as per the code below

    
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
    function yourprefix_register_demo_metabox() {
    
    	$cmb = new_cmb2_box( array(
            'id'      => 'images',
            'hookup'  => false,
            'show_on' => array(
                // These are important, don't remove
                'key'   => 'options-page',
                'value' => array( 'images' )
            ),
    	) );
    
    	$group_id = $cmb->add_field( array(
    		// Field Config...
    	) );
    $cmb->add_group_field( $group_id, ....);
    
    	// Additional fields...
    }
    

    In a separate page, show the form using
    cmb2_metabox_form("images", "images", array( 'cmb_styles' => true ) );

    Add more than 1 group and Save
    The page is reloaded with only the first group’s values. The others are discarded/not saved.

    Note: If I use the repeatable group inside a post/page by specifying

    object_types=array('post')

    it is able to capture all the group data

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you please provide the entirety of the config code you’re using? The additional fields area that you appear to be truncating could be playing a part in the issue, but I can’t confirm that until it’s all provided.

    Also is this part for a frontpage view? Say via the page.php template and viewing a page from the frontend? cmb2_metabox_form("images", "images", array( 'cmb_styles' => true ) );

    Thread Starter contactashish13

    (@rozroz)

    here is the entire config

    
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
    
    function yourprefix_register_demo_metabox() {
        $cmb = new_cmb2_box( array(
            'id'      => 'images',
            'show_on' => array(
                // These are important, don't remove
                'key'   => 'options-page',
                'value' => array( 'images' )
            ),
        ) );
    
    	$group_id  = $cmb->add_field( array(
    		'name'          => __( 'Image Type' ),
    		'id'            => 'images',
    		'type'          => 'group',
            'repeatable'    => true,
            'sortable'      => true,
            'options'       => array(
                'group_title'   => __( 'Image {#}' ),
                'add_button'    => __( 'Add Image' ),
                'remove_button' => __( 'Remove Image' ),
            ),
    	) );
    	$cmb->add_group_field( $group_id, array(
    		'name'      => __( 'Name' ),
    		'id'        => 'name',
    		'type'      => 'text',
    	) );
     }
    

    I’m showing the form using cmb2_metabox_form on the back end, in a settings screen under WP Admin. The intent is to save multiple image types, each with its name.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Sorry to sound a bit nitpicky, but this feels like it’s missing things still. May very well be my fault for not specifying as well, for the record.

    What I’m really hoping for is EVERY part of what wires up everything for you so that I can copy/paste into a php file and have a working options page, matching what you’re seeing. This up above, alone, isn’t setting up the options page like I’d need.

    Thanks for your patience and help.

    Thread Starter contactashish13

    (@rozroz)

    Ok, I’m not using the WP plugin but the cmb2 master branch from the repo. And I’m using this inside a custom plugin.

    I’m initializing CMB2 as follows in the plugin:

    
        // init cmb2
        add_action( 'init', array( 'admin_init' ) );
        function admin_init() {
            include_once 'cmb2.php'; 
        }
    
        // settings page
            add_action( 'admin_menu', array( 'admin_menu' ) );
        function admin_menu() {
            add_options_page( "name", "name", "manage_options", "slug", array( "admin_settings" ) );
        }
    
        function admin_settings() {
            include_once "settings.php";
        }
    

    cmb2.php contains the following code

    
    if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
    	require_once dirname( __FILE__ ) . '/cmb2/init.php';
    } elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
    	require_once dirname( __FILE__ ) . '/CMB2/init.php';
    }
    
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
    
    function yourprefix_register_demo_metabox() {
        $cmb = new_cmb2_box( array(
            'id'      => 'images',
            'show_on' => array(
                // These are important, don't remove
                'key'   => 'options-page',
                'value' => array( 'images' )
            ),
        ) );
    
    	$group_id  = $cmb->add_field( array(
    		'name'          => __( 'Image Type' ),
    		'id'            => 'images',
    		'type'          => 'group',
            'repeatable'    => true,
            'sortable'      => true,
            'options'       => array(
                'group_title'   => __( 'Image {#}' ),
                'add_button'    => __( 'Add Image' ),
                'remove_button' => __( 'Remove Image' ),
            ),
    	) );
    	$cmb->add_group_field( $group_id, array(
    		'name'      => __( 'Name' ),
    		'id'        => 'name',
    		'type'      => 'text',
    	) );
     }
    

    settings.php contains the below piece of code

    
    cmb2_metabox_form("images", "images", array( 'cmb_styles' => true ) );
    

    I hope this helps.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    So I was finally able to get a complete setup going, thank you for that.

    However, I wasn’t able to recreate any issue with saving.

    URL/settings page that shows up: /wp-admin/options-general.php?page=slug

    Creates a page that looks like https://cloudup.com/chZwDYk0z_2

    I’m able to add multiple individual image name fields (before save). https://cloudup.com/cPcsWD_0omm

    And it looks the same after hitting save https://cloudup.com/cUucomAIbcx

    Not seeing any javascript errors that may be contributing. So I’m having to wonder if there’s something else in your own install that is running interference.

    The only difference I can potentially see, but logically shouldn’t be playing a part, is that I don’t bootstrap CMB2 from my custom plugin below. I relied on the standalone plugin version that I activated.

    My pieced together version based on provided code above: https://cloudup.com/cQi4KrRy6rp

    Thread Starter contactashish13

    (@rozroz)

    Thanks. I wrote a simple barebones plugin to figure out where the problem could be in my wiring and I traced the problem to this

    
        <table class="form-table">
    <?php cmb2_metabox_form( "images", "images", array( 'cmb_styles' => true ) ); ?>
        </table>
    

    If I remove the container table, it works perfectly. But if the table stays, only the first value gets saved.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Interesting. I wonder if details regarding the Javascript are getting “confused” by the extra table markup. I do believe CMB2 handles the table markup itself to match WordPress standard settings page markup, so probably best to leave the table tag out. I also wonder if part of the issue is the lack of table row/table cell markup.

    Nonetheless, it sounds like you got things worked out at the moment, correct?

    Thread Starter contactashish13

    (@rozroz)

    Yeah its all good now. Thanks! ??

    Plugin Author Justin Sternberg

    (@jtsternberg)

    If you’re looking to do options/admin pages, there’s some pretty good starts for you in the snippet library: https://github.com/CMB2/CMB2-Snippet-Library/tree/master/options-and-settings-pages

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Repeatable group using cmb2_metabox_form saves only the first group’ is closed to new replies.