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