• Resolved _drg_

    (@_drg_)


    I have added two metaboxes to a custom post type using CMB2. I’d like to disable the CMB2 styles for one but not the other. I can’t seem to get that to work.

    For the one box, I’ve added 'cmb_styles' => false as an argument to the new_cmb2_box, and as arguments to the two add_fields calls as well. For the other box, I omit that argument. However, the style shows up for both.
    If I add code to disable the CMB2 styles for the whole site, then both metaboxes do not have the style.

    So, does the cmb_styles argument just affect the loading of the stylesheet (meaning if any cmb2 box/field has it, it’s loaded for everyone), or does it affect adding style names/classes to the elements?

    I am sure I’m doing something wrong. Any pointers would be helpful. Thank you!

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

    (@tw2113)

    The BenchPresser

    Can you provide the CMB2 configuration you’re using for everything?

    Thread Starter _drg_

    (@_drg_)

    Wordpress 4.9.1
    CMB2 2.3.0
    SportsPress 2.5.4
    PHP Version: 7.0.21
    MySQL Version: 5.7.19-log
    Let me know what else I can provide to help.

    Here are the results of some testing.

    With just the one CMB2 metabox on the post (and no other CMB2 boxes anywhere), the control will honor the cmb_styles directive (as expected), and without the cmb_styles argument, it will show the style (as expected).

    When I add another CMB2 metabox on the post; if both have no cmb_styles arg, then both show the cmb2 style. If both have the cmb_styles=false, then neither show the style.
    However, if either one has the argument and the other doesn’t, both show the cmb2 style.

    Additionally, I noticed that if I only have the one cmb2 metabox on the post, and have the styles=false argument, it won’t show (as expected), BUT if I just add the cmb_examples.php with an include in functions.php), the one metabox on my post shows the cmb2 style regardless of the argument.

    Thanks again for your time and help!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    For my previous reply, I meant all the code used to set up and create your metaboxes/option pages/etc so that I could try recreating on my own with what you’re using.

    I have to wonder if it’s a case of order mattering. For example, if you set it to false in one, and then create a new one right afterwards, and leave that one true, then it ultimately resolves to true. So I’m curious if there’s something that could be done to conditionally load the individual metabox registrations, so that they can be independent of each other.

    Thread Starter _drg_

    (@_drg_)

    Sorry for the misunderstanding – i’ve posted the code below.
    I did try changing the order of the metaboxes (the add action) with no apparent affect.

    <?php
    add_action( 'cmb2_admin_init', 'ohl_usahockey_metaboxes' );
    
    function ohl_usahockey_metaboxes() {
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_ohl_';
    
    	/**
    	 * Create the scoresheet metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => 'ohl_usahockey',
    		'title'         => __( 'USA Hockey', 'ohl' ),
    		'object_types'  => array( 'sp_player', ), // Post type
    		'context'       => 'side',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		//'cmb_styles' => false, // false to disable the CMB stylesheet
    		// 'closed'     => true, // Keep the metabox closed by default
    	) );
    
    	$cmb->add_field( array(
    		'name' => esc_html__( 'Current USA Hockey #', 'ohl' ),
    		'id'   => $prefix . 'usahockey_current_number',
    		'type' => 'text_medium',
    		//'cmb_styles' => false, // false to disable the CMB stylesheet
    	) );
    
    	$cmb->add_field( array(
    		'name' => esc_html__( 'Expires', 'ohl' ),
    		'id'   => $prefix . 'usahockey_current_expires',
    		'type' => 'text_date',
    		'date_format' => 'm/d/yy',
    		//'cmb_styles' => false, // false to disable the CMB stylesheet
    	) );
    }
    
    add_action( 'cmb2_admin_init', 'ohl_player_reg_metabox' );
    
    function ohl_player_reg_metabox() {
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_ohl_';
    
    	$cmb_group = new_cmb2_box( array(
    		'id'           => $prefix . 'player_reg_metabox',
    		'title'        => esc_html__( 'Player Registrations', 'ohl' ),
    		'object_types' => array( 'sp_player' ),
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		//'cmb_styles' => false, // false to disable the CMB stylesheet
    		// 'closed'     => true, // Keep the metabox closed by default
    	) );
    
    	$group_field_id = $cmb_group->add_field( array(
    		'id'          => $prefix . 'playerreg_row',
    		'type'        => 'group',
    		//'description' => esc_html__( 'Generates reusable form entries', 'ohl' ),
    		'options'     => array(
    			'group_title'   => esc_html__( 'Registration {#}', 'ohl' ), // {#} gets replaced by row number
    			'add_button'    => esc_html__( 'Add Registration', 'ohl' ),
    			'remove_button' => esc_html__( 'Remove', 'ohl' ),
    			'sortable'      => true, // beta
    			// 'closed'     => true, // true to have the groups closed by default
    		),
    	) );
    
    	/**
    	 * Group fields works the same, except ids only need
    	 * to be unique to the group. Prefix is not needed.
    	 *
    	 * The parent field's id needs to be passed as the first argument.
    	 */
    	$cmb_group->add_group_field( $group_field_id, array(
    		'name'       => esc_html__( 'Season', 'ohl' ),
    		'id'         => $prefix . 'playerreg_season',
    		'type'       => 'text',
    		// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
    	) );
    
    	$cmb_group->add_group_field( $group_field_id, array(
    		'name'             => esc_html__( 'Payment Status', 'cmb2' ),
    		'id'               => $prefix . 'playerreg_payment',
    		'type'             => 'radio',
    		//'show_option_none' => 'No Selection',
    		'options'          => array(
    			'standard' => esc_html__( 'None', 'cmb2' ),
    			'custom'   => esc_html__( 'Partial', 'cmb2' ),
    			'none'     => esc_html__( 'Paid in Full', 'cmb2' ),
    		),
    	) );
    
    	$cmb_group->add_group_field( $group_field_id, array(
    		'name'    => esc_html__( 'Registration Documents', 'cmb2' ),
    		'id'      => $prefix . 'playerreg_docs',
    		'type'    => 'multicheck',
    		// 'multiple' => true, // Store values in individual rows
    		'options' => array(
    			'regform' => esc_html__( 'Registration Form', 'cmb2' ),
    			'waiver' => esc_html__( 'Waiver', 'cmb2' ),
    			'usahockey' => esc_html__( 'USA Hockey', 'cmb2' ),
    		),
    		// 'inline'  => true, // Toggles display to inline
    	) );
    }
    
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    No worries.

    Looking things over and will report back with anything I find or if I have more questions.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Had a chance to look things over, friday night coder yo.

    Anyways, some thoughts.

    You can combine the two callbacks. Each use of new_cmb2_box( ... ) doesn’t need their own dedicated callback on cmb2_admin_init. They can all go in one. I’d end up assigning each call to new_cmb2_box() to a unique variable name, just to be safe.

    The cmb_styles parameter only goes in as part of the new_cmb2_box() arguments. It doesn’t go on the individual fields.

    Also the styles detail is not individual and/or isolated to each metabox. The parameter tells WP and CMB2 whether or not to enqueue the associated stylesheet that handles CMB2 styles. This would explain why you’re getting the results you are. The moment one is set to true, the sheet loads and affects all rendered metaboxes.

    Depending on exactly how much you’re wanting to do, it may be best to override the styles in question with your own. Just a quick thought.

    Hope this provides some insight and possible direction.

    Thread Starter _drg_

    (@_drg_)

    Thanks for taking the time to look into this and for the response!

    If I am reading it correctly, then I think you answered my initial question.

    So, does the cmb_styles argument just affect the loading of the stylesheet (meaning if any cmb2 box/field has it, it’s loaded for everyone), or does it affect adding style names/classes to the elements?

    The argument just affects whether the stylesheet loads (all or nothing).

    Some feedback:
    Because cmb_styles is an argument/option at the metabox level (i.e. when you create a metabox) it seems to imply that the option applies to that metabox, and can be different for each metabox. However, regardless of how many metaboxes are on a page, if one has cmb_styles=true (or all but one have cmb_styles=false), then the result is as if all of them were =true.

    I am not a big coder, so dont’ know the implications of this, but:
    A) Given it’s current behavior, it would make sense to me if cmb_tyles was an independent setting (not by metabox),
    OR,
    B) It would make sense if the argument was per metabox, and that the metabox html that is rendered would just omit the style parameters if cmb_styles=false.

    I like the idea of having it be specific to each metabox so I’d submit a feature request for #B.

    Either way, CMB2 has been a huge help to what I am working on. Thanks for all your hard work!

    Thread Starter _drg_

    (@_drg_)

    Another random thought. What if you added a cmb_styles_prefix option for creating metaboxes. Then, in the code that renders the html, it could prepend that text to each of the styles it puts out. You could then load the cmb style sheet no matter what.
    By default, the cmb_styles_prefix would be blank, and therefore the styles would match the cmb stylesheet. Otherwise, the styles would have new names and not rely on the stylesheet, and would allow someone to create custom styles where needed.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Valid thoughts and ideas, by all means.

    Opened an enhancement issue: https://github.com/CMB2/CMB2/issues/1085

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Trying to get cmb_styles options working’ is closed to new replies.