-
Notifications
You must be signed in to change notification settings - Fork 147
/
cmb2_init_before_hookup-add-fields.php
56 lines (46 loc) · 1.86 KB
/
cmb2_init_before_hookup-add-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* This file demonstrates adding new fields to registered CMB2 metaboxes/objects
*/
function yourprefix_add_new_field_in_3rd_position() {
$prefix = '_yourprefix_demo_';
// Retrieve a CMB2 instance
$cmb = cmb2_get_metabox( '_yourprefix_demo_metabox' );
// This should return false because we don't have a '_yourprefix_demo_text2' field
$field_id = $cmb->update_field_property( '_yourprefix_demo_text2', 'type', 'text' );
/**
* Since '_yourprefix_demo_text2' doesn't exist, Let's create it.
* Always need to compare this value strictly to false, as a field_id COULD be 0 or ''
*/
if ( false === $field_id ) {
$cmb->add_field(
// Normal field setup
array(
'name' => __( 'Test Text 2', 'your_textdomain' ),
'desc' => __( 'Test Text 2 description', 'your_textdomain' ),
'id' => '_yourprefix_demo_text2',
'type' => 'text',
'attributes' => array( 'placeholder' => __( "I'm some placeholder text", 'your_textdomain' ) ),
),
3 // Insert this field in the third position
);
}
}
add_action( 'cmb2_init_before_hookup', 'yourprefix_add_new_field_in_3rd_position' );
function yourprefix_add_new_field_to_group() {
// Try to get a metabox w/ the id of '_yourprefix_group_metabox'
if ( $cmb_group_demo = cmb2_get_metabox( '_yourprefix_group_metabox' ) ) {
$cmb_group_demo->add_group_field(
array(
'name' => __( 'Test Text 2', 'your_textdomain' ),
'desc' => __( 'field description (optional)', 'your_textdomain' ),
'id' => 'text2',
'type' => 'text',
'attributes' => array( 'placeholder' => __( "I'm some placeholder text", 'your_textdomain' ) ),
),
'_yourprefix_group_demo', // Add this to the _yourprefix_group_demo group field
2 // And insert it into the 2nd position
);
}
}
add_action( 'cmb2_init_before_hookup', 'yourprefix_add_new_field_to_group' );