• Resolved teamcrisis

    (@teamcrisis)


    Per your request Issue #261 I am posting this here.

    I’m trying to add fields via a foreach:

    add_action( 'cmb2_init', 'yourprefix_register_build_metabox' );
    
    function yourprefix_register_build_metabox() {
    
    	$post_id = isset($_GET['post']) ? $_GET['post'] : false;
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_yourprefix_builder_';
    
    	/**
    	 * Metabox to be displayed on a single page ID
    	 */
    	$cmb_about_page = new_cmb2_box( array(
    		'id'           => $prefix . 'metabox',
    		'title'        => __( 'Build Metaboxes', 'cmb2' ),
    		'object_types' => array( 'page', ), // Post type
    		'context'      => 'normal',
    		'priority'     => 'high',
    		'show_names'   => TRUE, // Show field names on the left
    
    	) );
    
    	$cmb_about_page->add_field( array(
    		'name' => __( 'Test Text', 'cmb2' ),
    		'desc' => __( 'field description (optional)', 'cmb2' ),
    		'id'   => $prefix . 'text',
    		'type' => 'multicheck',
    		'options' => array(
    			'check1' => __( 'Check One', 'cmb2' ),
    			'check2' => __( 'Check Two', 'cmb2' ),
    			'check3' => __( 'Check Three', 'cmb2' ),
    		),
    	) );
    
    	if ( $post_id ) {
    
    		// Helper to find key label
    		function search_for_meta_value( $value, $array ) {
    			foreach ( $array as $key => $val ) {
    				if ( $val['value'] === $value ) {
    					return $val['label'];
    				}
    			}
    
    			return NULL;
    		}
    
    		$builder_meta_boxes = get_post_meta( $post_id, '_yourprefix_builder_text', TRUE );
    
    		if ( isset($builder_meta_boxes) && is_array($builder_meta_boxes)) {
    
    			$text = get_post_meta( $post_id, '_yourprefix_builder_text', true );
    
    			foreach($builder_meta_boxes as $value => $meta_box_name){ 
    
    				$cmb_about_page->add_field( array(
    				   'name'        => $meta_box_name . ' Field',
    				   'desc'        => 'Set your value.',
    				   'id'          => $prefix . $meta_box_name,
    				   'type'        => 'checkbox',
    				));
    			}
    		}
    	}
    }

    The fields add without an issue, but the values do not save. Any idea why? I’m not seeing any info in the wiki about this. Did I simply miss it?

    https://wordpress.org/plugins/cmb2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    Yes, your values do not save because you’re wrapping field registration in a condition that checks for $_GET['post'] which does not exist during the save process. You should use the show_on_cb field (or metabox) parameter to conditionally display the fields.

    Thread Starter teamcrisis

    (@teamcrisis)

    What I’m attempting to do is add additional metaboxes based on the checkbox selection of a user. On $post_id = 1 they make make 5 selections, but on $post_id = 2 they may only make 1 selection. In this case, 5 metaboxes and 1 metabox respectively, may be added to the page on save.

    Is this not possible to do with CMB?

    Thread Starter teamcrisis

    (@teamcrisis)

    Looks like doing a check for $_GET[‘post’] and $_POST[‘post_ID’] may do what I need. I’ll update if it doesn’t work for what I’m trying to accomplish.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    teamcrisis, did this end up working itself out in the end?

    Was looking for the same thing and yes , seems the checking of $_POST or $_GET does the work .

    if(isset($_GET['post'])){
    	$postID = $_GET['post'];
    }
    
    if(isset($_POST['post_ID'])){
    	$postID = $_POST['post_ID'];
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_field via foreach’ is closed to new replies.