Contents
Overview
This Default Field Type is available in WordPress Creation Kit free version. The Checkbox Field creates a list of thickbox options. A Checkbox Field can be used to get Yes / No responses from the user.
Creating a Checkbox Field
To add a Checkbox Field to a previously created Custom Meta Box, under the Meta Box Fields tab simply insert a Field Title and under Field Type make sure you select Checkbox.
The Checkbox Field contains options to customize it:
- Description – Allows you to specify a description for the Checkbox Field
- Required – Select whether the field is required or not
- Default Value – Set a default value for the Checkbox Field. In case of multiple values separate them with “,”
- Options – For multiple options separate them with a “,”
- Labels – For multiple labels separate them with a “,”
Page, Post or Custom Post Type Edit Screen
This is how the Checkbox Field we created above looks like in the Edit Screen:
Template usage
The following examples are for a Custom Meta Box with the “Group Name” argument “my_meta_name“. Make sure to replace this with the “Group Name” you have set up. The Custom Meta Box below is setup to be attached to a post.
Using the WCK Custom Fields API
The Checkbox Field returns a string or an array.
For a Single Meta Box
To output a value inside the loop we use the function the_cfc_field() which for the Checkbox Field echoes a string with all the selected values separated by comma.
1 | Colors: <!--?php the_cfc_field('my_meta_name', 'colors'); ?--> |
To assign the value to a variable we use the function get_cfc_field() which for the Checkbox Field returns an array of all the selected values:
1 | <!--?php $colors_array = get_cfc_field('my_meta_name', 'colors'); ?--> |
For a Repeater Meta Box
To output all the “Colors” entries from the repeater field we use the functions get_cfc_meta() and the_cfc_field():
1 | <!--?php foreach( get_cfc_meta( 'my_meta_name' ) as $key => $value ){ the_cfc_field( 'my_meta_name','colors', false, $key ); } ?--> |
To output a specific “Colors” entry from the repeater field (e.g. the second entry), we use the function the_cfc_field():
1 | <!--?php the_cfc_field( 'my_meta_name','colors', false, 1 ); ?--> |
The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…
Using the default WordPress functions
The Checkbox Field returns a string or an array.
For a Single Meta Box
1 | <!--?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta[0]['colors'] ) ) echo 'Value:'.$my_meta[0]['colors']; ?--> |
For a Repeater Meta Box
To output all the “Colors” entries in the repeater field:
1 | <!--?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta ) ){ foreach( $my_meta as $entry ){ echo $entry['colors']; } } ?--> |
To output a specific “Colors” entry from the repeater field (e.g. the second entry):
1 | <!--?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta[1]['colors'] ) ) echo $my_meta[1]['colors']; ?--> |
The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…