Skip Navigation

Cannot output checkboxes field option titles

Open

Symptoms

Checkboxes fields have options that include the value to be saved, and the title to displayed when outputting the checked options.

The latter is not working, the saved values are being output instead of the option titles.

This affects the output of checkboxes fields using the Single Field block, or the corresponding types shortcode, or the function types_render_field.

Workaround

The developers are aware of the problem and will provide a fix.

In the meantime, a workaround is to explicitly specify what to display for each checked/unchecked option where the field is being output (e.g. in the block settings).

Alternately, you could register the following custom shortcode “checkboxes”.


add_shortcode( 'checkboxes', function( $atts = [] ){

// provide defaults
$atts = shortcode_atts(
array(
'field'=> 'null',
'separator' => ', '
),
$atts
);

$output = "";
global $post;

$field_settings = types_get_field( $atts['field'] );

$field_values = get_post_meta( $post->ID, 'wpcf-'.$atts['field'], false );
$field_value_options = array_keys( $field_values[0] );

$separator = "";
foreach ($field_value_options as $field_value_option) {

$field_value_option_title = $field_settings['data']['options'][$field_value_option]['title'];
$output .= $separator . $field_value_option_title;

$separator = $atts['separator'];
}

return $output;
});

Add the code to Toolset > Settings > Custom Code, and then use the code like so:


[checkboxes field='checkers' separator='|']

The field slug is required, the separator argument is optional and will default to “, “

Leave
a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>