• Resolved Jonathan Stegall

    (@jonathanstegall)


    I’m trying to set a default value on a multicheck field using the default_cb parameter as a callback, but it looks like the callback method is not being called.

    Here’s what I have:

    
    $user_preferences = new_cmb2_box( array(
    	'id'            => 'reading_preferences',
    	'title'         => 'Reading Preferences',
    	'object_types'  => 'user',
    	'context'       => 'normal',
    	'priority'      => 'low',
    ) );
    $user_preferences->add_field( array(
    	'name'    => 'Subscribe to these regular newsletters:',
    	'desc'    => '',
    	'id'      => '_newsletters',
    	'type'    => 'multicheck',
    	'options_cb' => 'user_mc_newsletter_options',
    	'default_cb' => 'mp_set_checkbox_default',
    ) );
    function mp_set_checkbox_default( $field_args, $field ) {
    	error_log( 'field args are ' . print_r( $field_args, true ) );
    	error_log( 'field is ' . print_r( $field, true ) );
    	return 'what';
    }
    

    My options_cb callback works fine, but the default_cb doesn’t do anything. That is, it doesn’t even log an error in the above example. I’ve been checking in CMB2_Field.php to see if this parameter is ignored on multicheck, but I haven’t seen that it is. Should this be possible?

    Thanks.

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

    (@tw2113)

    The BenchPresser

    Here’s an example I have working:

    add_action( 'cmb2_admin_init', 'cmb_pages_customtext_metabox' );
    function cmb_pages_customtext_metabox() {
    	$user_preferences = new_cmb2_box( array(
    		'id'           => 'reading_preferences',
    		'title'        => 'Reading Preferences',
    		'object_types' => 'user',
    		'context'      => 'normal',
    		'priority'     => 'low',
    	) );
    	$user_preferences->add_field( array(
    		'name'       => 'Subscribe to these regular newsletters:',
    		'desc'       => '',
    		'id'         => '_newsletters',
    		'type'       => 'multicheck',
    		'options_cb' => 'user_mc_newsletter_options',
    		'default_cb' => 'mp_set_checkbox_default',
    	) );
    }
    function user_mc_newsletter_options( $field ){
    	return [
    		'check1' => 'Check One',
    		'check2' => 'Check Two',
    		'check3' => 'Check Three',
    	];
    }
    
    function mp_set_checkbox_default( $field_args, $field ) {
    	return 'check3';
    }
    

    Not quie sure what you may have in your user_mc_newsletter_options callback, so I improvised with what is available on the CMB2 wiki on GitHub.

    https://cloudup.com/cL47ntqGRM3

    Hopefully it points you in the right direction.

    Thread Starter Jonathan Stegall

    (@jonathanstegall)

    I’ve noticed that this works on admin pages, but not on the front end. That is, if I use cmb2_admin_init as the hook, it works as your screenshot indicates, but if I use cmb2_init it does not.

    I’ve been using this box on the front end include file. It displays the checkboxes as expected (using the options_cb callback, but it never calls the default_cb callback.

    
    <fieldset>
    	<?php
    	$args = array(
    		'save_button' => esc_html__( 'Save Changes', 'cmb2' ),
    		'cmb_styles'  => false,
    		'enqueue_js'  => false,
    	);
    	echo cmb2_get_metabox_form( 'user_reading_preferences', $attributes['user']->ID, $args );
    	?>
    </fieldset>
    

    And again, the meta box is on cmb2_init. This has worked as it should, otherwise. I can see the options, correctly update the user’s data for this field, etc. It just doesn’t work with the default callback.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you provide all of the code you’re using? also just to make sure, all the callback functions you’re making for it. If it’s bundled up as a custom plugin, even better, though not required. Just trying to make it as easily installable as possible, to recreate and test with.

    Thread Starter Jonathan Stegall

    (@jonathanstegall)

    Sure. I don’t have it as a plugin, it’s just in theme files. CMB2 is installed as a plugin.

    This code is in functions.php:

    
    // add fields to users
    if ( ! function_exists( 'cmb2_user_fields' ) ) :
    	add_action( 'cmb2_init', 'cmb2_user_fields' );
    	function cmb2_user_fields() {
    
    		$user_preferences = new_cmb2_box( array(
    			'id'            => 'user_reading_preferences',
    			'title'         => 'Reading Preferences',
    			'object_types'  => array( 'user' ),
    			'context'       => 'normal',
    			'priority'      => 'low',
    		) );
    
    		$user_preferences->add_field( array(
    			'name'    => 'Subscribe to these regular newsletters:',
    			'desc'    => '',
    			'id'      => '_newsletters',
    			'type'    => 'multicheck',
    			'options_cb' => 'user_mc_newsletter_options',
    			'default_cb' => 'mp_set_checkbox_default',
    		) );
    	}
    endif;
    
    function mp_set_checkbox_default( $field_args, $field ) {
    	error_log( 'field args are ' . print_r( $field_args, true ) );
    	error_log( 'field is ' . print_r( $field, true ) );
    	return 'check3';
    }
    
    function user_mc_newsletter_options( $field ) {
    	return [
    		'check1' => 'Check One',
    		'check2' => 'Check Two',
    		'check3' => 'Check Three',
    	];
    }
    

    I have a custom shortcode that I’m using to render this form. It works like this:

    
    if ( ! function_exists( 'account_preferences_form' ) ) :
    	add_shortcode( 'custom-account-preferences-form', 'account_preferences_form' );
    	function account_preferences_form( $attributes, $content = null ) {
    
    		if ( ! is_array( $attributes ) ) {
    			$attributes = array();
    		}
    
    		$user_id = get_query_var( 'users', '' );
    		if ( isset( $_GET['user_id'] ) ) {
    			$user_id = esc_attr( $_GET['user_id'] );
    		} else {
    			$user_id = get_current_user_id();
    		}
    
    		if ( ! is_user_logged_in() ) {
    			return __( 'You are not signed in.', 'user-account-management' );
    		} else {
    			//$attributes['login'] = rawurldecode( $_REQUEST['login'] );
    			// Error messages
    			$errors = array();
    			if ( isset( $_REQUEST['errors'] ) ) {
    				$error_codes = explode( ',', $_REQUEST['errors'] );
    
    				foreach ( $error_codes as $code ) {
    					$errors[] = $this->get_error_message( $code );
    				}
    			}
    			$attributes['errors'] = $errors;
    			if ( isset( $user_id ) && '' !== $user_id ) {
    				$attributes['user'] = get_userdata( $user_id );
    			} else {
    				$attributes['user'] = wp_get_current_user();
    			}
    			$attributes['user_meta'] = get_user_meta( $attributes['user']->ID );
    
    			return get_template_html( 'account-preferences-form', 'front-end', $attributes );
    
    		}
    	}
    endif;
    
    function get_template_html( $template_name, $location = '', $attributes = null ) {
    	if ( ! $attributes ) {
    		$attributes = array();
    	}
    
    	if ( '' !== $location ) {
    		$location = $location . '/';
    	}
    
    	ob_start();
    
    	$file = get_theme_file_path() . '/' . $this->slug . '-templates/' . $location . $template_name . '.php';
    	require( $file );
    
    	$html = ob_get_contents();
    	ob_end_clean();
    
    	return $html;
    }
    

    Then in my form (the template file), I have this:

    
    <fieldset>
    	<?php
    	$args = array(
    		'save_button' => esc_html__( 'Save Changes', 'cmb2' ),
    		'cmb_styles'  => false,
    		'enqueue_js'  => false,
    	);
    	echo cmb2_get_metabox_form( 'user_reading_preferences', $attributes['user']->ID, $args );
    	?>
    </fieldset>
    

    In the browser, it renders like this:

    
    <fieldset>
    	<input name="object_id" value="1" type="hidden">
    	<!-- Begin CMB2 Fields -->
    	<input id="nonce_CMB2phpuser_reading_preferences" name="nonce_CMB2phpuser_reading_preferences" value="ca980bbe8a" type="hidden">
    	<div class="cmb2-wrap form-table">
    		<div id="cmb2-metabox-user_reading_preferences" class="cmb2-metabox cmb-field-list">
    			<div class="cmb-row cmb-type-multicheck cmb2-id--newsletters" data-fieldtype="multicheck">
    				<div class="cmb-th">
    					<label for="_newsletters">Subscribe to these regular newsletters:</label>
    				</div>
    				<div class="cmb-td">
    				<ul class="cmb2-checkbox-list cmb2-list">
    					<li>
    						<input class="cmb2-option" name="_newsletters[]" id="_newsletters1" value="check1" type="checkbox">
    						<label for="_newsletters1">Check One</label>
    					</li>
    					<li>
    						<input class="cmb2-option" name="_newsletters[]" id="_newsletters2" value="check2" type="checkbox">
    						<label for="_newsletters2">Check Two</label>
    					</li>
    					<li>
    						<input class="cmb2-option" name="_newsletters[]" id="_newsletters3" value="check3" type="checkbox">
    						<label for="_newsletters3">Check Three</label>
    					</li>
    				</ul>
    			</div>
    		</div>
    	</div>
    </div>
    <!-- End CMB2 Fields -->
    <input name="submit-cmb" value="Save Changes" class="button-primary" type="submit">
    </fieldset>
    

    My error logs are empty, since the default callback isn’t called.

    I hope that’s helpful?

    • This reply was modified 8 years, 4 months ago by Jonathan Stegall. Reason: pasted the wrong method
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Still not managing to recreate, only difference I can see is that i’m putting things in an mu-plugins file which runs a bit earlier than functions.php, but that shouldn’t make a difference.

    Haven’t tried saving, but I don’t think that’s been part of the issue either.

    Thread Starter Jonathan Stegall

    (@jonathanstegall)

    You’re right. I tried putting this into a default wordpress running 2017, and it works fine. There is clearly something else in my theme or other plugins that I’m running, rather than something in this plugin. I’ll go ahead and mark this as resolved. Thanks.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Feel free to circle back and let us know what the conflict source is. I’m kind of curious now.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Using default_cb on a multicheck field’ is closed to new replies.