add_field via foreach
-
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?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘add_field via foreach’ is closed to new replies.