forked from jcchavezs/cmb2-conditionals
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-functions.php
executable file
·185 lines (167 loc) · 5.3 KB
/
example-functions.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
*
* Be sure to replace all instances of 'yourprefix_' with your project's prefix.
* http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
*
* @category YourThemeOrPlugin
* @package Demo_CMB2_Conditionals
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/jcchavezs/cmb2-conditionals
*/
/**
* Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
*/
define('CMB2_DIR', WP_CONTENT_DIR . '/cmb2');
if ( file_exists( CMB2_DIR . '/cmb2/init.php' ) ) {
require_once CMB2_DIR . '/cmb2/init.php';
} elseif ( file_exists( CMB2_DIR . '/CMB2/init.php' ) ) {
require_once CMB2_DIR . '/CMB2/init.php';
}
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
/**
* Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook.
*/
function yourprefix_register_demo_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_demo_';
/**
* Sample metabox to demonstrate each field type included
*/
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => 'Test Metabox',
'object_types' => array( 'page', ), // Post type
) );
$cmb_demo->add_field( array(
'name' => 'Address',
'desc' => 'Write down an address for showing the other address options',
'id' => $prefix . 'address',
'type' => 'text',
'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
// 'on_front' => false, // Optionally designate a field to wp-admin only
// 'repeatable' => true,
) );
$cmb_demo->add_field( array(
'name' => 'Zipcode',
'id' => $prefix . 'zipcode',
'type' => 'text_medium',
// 'repeatable' => true,
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'address',
)
) );
$cmb_demo->add_field( array(
'name' => 'Country',
'id' => $prefix . 'country',
'type' => 'text_medium',
// 'repeatable' => true,
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'address',
)
) );
$cmb_demo->add_field( array(
'name' => 'Checkbox',
'id' => $prefix . 'checkbox',
'type' => 'checkbox',
) );
$cmb_demo->add_field( array(
'name' => 'Show if checked',
'id' => $prefix . 'show_if_checked',
'type' => 'text',
'attributes' => array(
'data-conditional-id' => $prefix . 'checkbox',
// works too: 'data-conditional-value' => 'on',
)
) );
$cmb_demo->add_field( array(
'name' => 'Show if unchecked',
'id' => $prefix . 'show_if_unchecked',
'type' => 'text',
'attributes' => array(
'data-conditional-id' => $prefix . 'checkbox',
'data-conditional-value' => 'off',
)
) );
$cmb_demo->add_field( array(
'name' => 'Reason',
'id' => $prefix . 'reason',
'type' => 'select',
'show_option_none' => true,
'options' => array(
'one' => 'Reason 1',
'two' => 'Reason 2',
'three' => 'Reason 3',
'other' => 'Other reason'
),
) );
$cmb_demo->add_field( array(
'name' => 'Other reason detail',
'desc' => 'Write down the reason',
'id' => $prefix . 'other_reason_detail',
'type' => 'textarea',
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'reason',
'data-conditional-value' => 'other',
)
) );
$cmb_demo->add_field( array(
'name' => 'Reason 2',
'id' => $prefix . 'reason_2',
'type' => 'select',
'show_option_none' => true,
'options' => array(
'one' => 'Reason 1',
'two' => 'Reason 2',
'three' => 'Reason 3',
'other_price' => 'Other reason based on the price',
'other_quality' => 'Other reason based on the quality'
),
) );
$cmb_demo->add_field( array(
'name' => 'Other reason detail',
'desc' => 'Write down the reason',
'id' => $prefix . 'other_reason_detail_2',
'type' => 'textarea',
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'reason_2',
'data-conditional-value' => json_encode(array('other_price', 'other_quality'))
)
) );
$cmb_demo->add_field( array(
'name' => 'Sizes',
'id' => $prefix . 'sizes',
'type' => 'radio',
'show_option_none' => true,
'options' => array(
'xs' => 'XS',
's' => 'S',
'm' => 'M',
'l' => 'L',
'xl' => 'XL',
'custom' => 'Custom'
),
'attributes' => array(
'required' => 'required',
)
) );
$cmb_demo->add_field( array(
'name' => 'Custom description',
'desc' => 'Write a description for your custom size',
'id' => $prefix . 'size_custom_description',
'type' => 'textarea',
'required' => true,
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'sizes',
'data-conditional-value' => 'custom',
)
) );
}