• Resolved vanlueckn

    (@vanlueckn)


    Hi,

    i created a cmb2 multicheck field like this:

    $fieldId = $cmb->add_field( array(
    		'name'    => 'Ziele',
    		'desc'    => 'Ziele mit denen dieser Beitrag synchronisiert werden soll.',
    		'id'      => 'vlck_field_sync_to',
    		'type'    => 'multicheck',
    		'options' => $sites,
    	) );

    Now i have to update the options of the multicheck programmatically in different methods in my functions.php (at runtime).

    How can i update the options of my field after creation at runtime?

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

    (@tw2113)

    The BenchPresser

    When you say update the options, you mean update what ends up stored in that $sites variable?

    How are you populating the variable in the first place? Custom function call? Static list that you change each time needed? Somehow else?

    Thread Starter vanlueckn

    (@vanlueckn)

    Hi`Hello,

    thank you very much for the answer.

    I create the custom field as follows:

    add_action( 'cmb2_admin_init', 'cmb2_metaboxes' );
    /**
     * Define the metabox and field configurations.
     */
    function cmb2_metaboxes() {
    
    	/**
    	 * Initiate the metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'           => 'vlck_syncbox',
    		'title'        => 'Synchronisation - Einstellungen',
    		'object_types' => array( 'post', ), // Post type
    		'context'      => 'normal',
    		'priority'     => 'high',
    		'show_names'   => true,
    	) );
    
    	$sites       = [];
    	$sitesOption = get_option( 'wp_api_mcs_sites' );
    	if ( $sitesOption ) {
    		foreach ( $sitesOption as $url => $site ) {
    			$sites[ $url ] = $url;
    		}
    	}
    
    	$fieldId = $cmb->add_field( array(
    		'name'    => 'Ziele',
    		'desc'    => 'Ziele mit denen dieser Beitrag synchronisiert werden soll.',
    		'id'      => 'vlck_field_sync_to',
    		'type'    => 'multicheck',
    		'options' => $sites,
    	) );
    
    	if ( $fieldId ) {
    		add_option( 'vlck_sync_field_id', $fieldId );
    	}
    }

    I then need to adjust the available options of the Custom Field in another method from time to time. For example, at the beginning I have only two check boxes in the multi-checkbox field, but later I need three or even four

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    From what I’m seeing, everything in this checkbox field is dependent on the data stored in the wp_api_mcs_sites option, which I assume CMB2 has zero control over, so there’s not really much we can do with that beyond what you’re already doing.

    The only way I can think of would be if there’s perhaps an API endpoint or something that’s being used for setting/saving the values in the option. Instead of storing to an option, just make the API request directly here and grab the desired parts at that moment. I know we have an options_cb where cb means callback, you could specify a function to invoke directly with it.

    https://github.com/CMB2/CMB2/wiki/Field-Types#optional

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Update CMB2 multicheck field options’ is closed to new replies.