• Resolved Wildcard

    (@thenewguy_14)


    SO when i am adding meta boxes in my function file do i need to add a function everytime i add a prefix? or do i just change the prefix to something like $prefix_two? Or am i doing it completely wrong. Here is my code for my home page meta boxes.

    /* ********* adding custom metaboxes ***************** */
    add_action( 'cmb2_admin_init', 'home_custom_metaboxes' );
    /**
     * Define the metabox and field configurations.
     */
    function home_custom_metaboxes() {
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_section_one_content';
    
    	/**
    	 * Initiate the metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => 'section_one_content',
    		'title'         => __( 'Section One Content', 'cmb2' ),
    		'object_types'  => array( 'page', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		'cmb_styles'    => true, // false to disable the CMB stylesheet
    		'closed'        => false, // Keep the metabox closed by default
    		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
    	) );
    
    	// main section headline
    	$cmb->add_field( array(
    		'name'       => __( 'Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the about section', 'cmb2' ),
    		'id'         => $prefix . '_text',
    		'type'       => 'text'
    	) );
    
    	// headline for first about section
    	$cmb->add_field( array(
    		'name'       => __( 'Content Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the first about section content', 'cmb2' ),
    		'id'         => $prefix . '_textarea',
    		'type'       => 'textarea'
    	) );
    
    	// Text for first about section
    	$cmb->add_field( array(
    		'name'       => __( 'Content Text', 'cmb2' ),
    		'desc'       => __( 'add some text for the first about section', 'cmb2' ),
    		'id'         => $prefix . '_textarea2',
    		'type'       => 'textarea'
    	) );
    	// image for first about section
    	$cmb->add_field( array(
    		'name' => __( 'Section Background Image', 'cmb2' ),
    		'desc' => __( 'Upload an image or enter a URL for the first about section background.', 'cmb2' ),
    		'id'   => $prefix . '_image',
    		'type' => 'file',
    	) );
    
    	// headline for second about section
    	$cmb->add_field( array(
    		'name'       => __( 'Content Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the second about section content', 'cmb2' ),
    		'id'         => $prefix . '_textarea3',
    		'type'       => 'textarea'
    	) );
    
    	// Text for second about section
    	$cmb->add_field( array(
    		'name'       => __( 'Content Text', 'cmb2' ),
    		'desc'       => __( 'add some text for the second about section', 'cmb2' ),
    		'id'         => $prefix . '_textarea4',
    		'type'       => 'textarea'
    	) );
    
    	// image for second about section
    	$cmb->add_field( array(
    		'name' => __( 'Section Background Image', 'cmb2' ),
    		'desc' => __( 'Upload an image or enter a URL for the second about section background.', 'cmb2' ),
    		'id'   => $prefix . '_image2',
    		'type' => 'file',
    	) );
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_section_two_content';
    
    	/**
    	 * Initiate the metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => 'section_two_content',
    		'title'         => __( 'Section Two Content', 'cmb2' ),
    		'object_types'  => array( 'page', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		'cmb_styles'    => true, // false to disable the CMB stylesheet
    		'closed'        => false, // Keep the metabox closed by default
    		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
    	) );
    
    	// main section headline
    	$cmb->add_field( array(
    		'name'       => __( 'Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the purveyors section', 'cmb2' ),
    		'id'         => $prefix . '_text',
    		'type'       => 'text'
    	) );
    
    	// purveyor section content headline
    	$cmb->add_field( array(
    		'name'       => __( 'Content Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the purveyors content section', 'cmb2' ),
    		'id'         => $prefix . '_text2',
    		'type'       => 'text'
    	) );
    
    	// Content text for purveyors section
    	$cmb->add_field( array(
    		'name'       => __( 'Content Text', 'cmb2' ),
    		'desc'       => __( 'add some text for the purveyors section', 'cmb2' ),
    		'id'         => $prefix . '_textarea',
    		'type'       => 'textarea'
    	) );
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_section_three_content';
    
    	/**
    	 * Initiate the metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => 'section_three_content',
    		'title'         => __( 'Section Three Content', 'cmb2' ),
    		'object_types'  => array( 'page', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		'cmb_styles'    => true, // false to disable the CMB stylesheet
    		'closed'        => false, // Keep the metabox closed by default
    		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
    	) );
    
    	// main section headline
    	$cmb->add_field( array(
    		'name'       => __( 'Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the Look Around Section', 'cmb2' ),
    		'id'         => $prefix . '_text',
    		'type'       => 'text'
    	) );
    
    	$cmb->add_field( array(
    		'name'         => __( 'Multiple Slide Images', 'cmb2' ),
    		'desc'         => __( 'Upload Images for Slides', 'cmb2' ),
    		'id'           => $prefix . '_file_list',
    		'type'         => 'file_list',
    		'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
    	) );
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_section_four_content';
    
    	/**
    	 * Initiate the metabox
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => 'section_four_content',
    		'title'         => __( 'Section Four Content', 'cmb2' ),
    		'object_types'  => array( 'page', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		'cmb_styles'    => true, // false to disable the CMB stylesheet
    		'closed'        => false, // Keep the metabox closed by default
    		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
    	) );
    
    	// main section headline
    	$cmb->add_field( array(
    		'name'       => __( 'Headline', 'cmb2' ),
    		'desc'       => __( 'add a headline for the Lets Talk About It Section', 'cmb2' ),
    		'id'         => $prefix . '_text',
    		'type'       => 'text'
    	) );
    
    	// Add other metaboxes as needed
    
    }

    i basically want a new meta box for each section so they can be opened and closed so i guess i just need to know if the code is set up right or if i am doing something wrong. Everything is working, I don’t want ot have any conflicts later on down the road. thanks for any help

    https://ww.wp.xz.cn/plugins/cmb2/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Wildcard

    (@thenewguy_14)

    Also i wanted the fields to save to the data base with a different prefixes for each section or should i have one prefix like home_section_ then just name each field with a unique id that way there is only one prefix being used? Just not sure what is the right way to do it cause i feel there are three ways to do it right now….

    1) have a different function for each section
    2) name the $prefix different like $prefix_two $prefix_three or
    3) have one prefix and just name the field with a unique id like
    $prefix . _first_section_text and so on

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Everything looks fine to me. Each time you use 'new_cmb2_box' you’re creating a new metabox, and then adding fields to it. You technically wouldn’t need to change the prefix each time, as that’s just something appended to the front of the meta_key for the database, for easier identification. You can if you want though, if it helps with your organization of content.

    You don’t need to do a different callback/hook for each, metabox, you can do as many metaboxes you want in one.

    Long story short, you should be just fine, and all of these different methods are more preference than anything.

    Thread Starter Wildcard

    (@thenewguy_14)

    cool thanks man i ended up just changing the $prefix to a different name like $prefix_two just for organizational preferences that way i have a different one for each meta box in the data base….your plugin rocks i cant be used for so much….

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Glad I could help confirm and put worries to rest.

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

The topic ‘General setup question’ is closed to new replies.