• Resolved newbars

    (@newbars)


    Hi guys, im using a theme where their custom metabox is make with the cmb2 plugin, what im trying to do is to replace a few of the custom fields, i search in your documentation but i didn’t find a good example

    What is the best way to do it? Maybe remove the function and added a new one in my child theme?

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

    (@tw2113)

    The BenchPresser

    Hi, assuming you’re not wanting to edit the theme parts themselves, perhaps it’s a parent theme that still gets updates, then I think this code may work out for you.

    add_action( 'cmb2_admin_init', function() {
    
    	$mb = cmb2_get_metabox( 'metabox_attachment' );
    
    	$mb->remove_field( 'wiki_test_repeat_group' );
    
    }, 10 );
    

    You’ll want to have this callback run AFTER the setup of the original metabox, so that it exists and is ready to be removed.

    You’ll need to pass in the created ID value for the metabox itself into the cmb2_get_metabox() function.

    From the example code I was testing with, that’d be the ID from this:

    $cmb = new_cmb2_box( [
    	'id'           => 'metabox_attachment',
    	'title'        => 'Test',
    	'object_types' => [ 'post' ], // Post type
    	'priority'     => 'high',
    	'show_names'   => true
    ] );
    

    The two fields that were added in my example code are:

    $cmb->add_field( array(
    	'id'          => 'wiki_test_repeat_group',
    	'type'        => 'text',
    	'description' => __( 'Generates reusable form entries', 'cmb2' ),
    	'repeatable'  => true,
    	'sortable'    => true,
    ) );
    
    $cmb->add_field( array(
    	'id'          => 'wiki_test_repeat_group2',
    	'type'        => 'text',
    	'description' => __( 'Wat', 'cmb2' ),
    ) );
    

    so for my initial code to you above, I ran $mb->remove_field( 'wiki_test_repeat_group' ); to remove that first field, and have only the second one render and load.

    Hope this helps.

    Thread Starter newbars

    (@newbars)

    Works wonderful, i just had to change priority from 10 to 15 and works fine

    Thanks so much for your help michael!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘remove default field from theme’ is closed to new replies.