wp-shortcake / shortcake

Shortcake makes using WordPress shortcodes a piece of cake.
GNU General Public License v2.0
664 stars 142 forks source link

Pass false shortcode value for unchecked checkbox #359

Open niallkennedy opened 9 years ago

niallkennedy commented 9 years ago

A shortcode includes a boolean attribute, do_the_thing, with a default state of true. A falsey attribute value overrides the default.

The shortcode author exposes the attribute to Shortcake through a checkbox field type, passing a value of true in string form, resulting in a checked property output in tmpl-shortcode-ui-field-checkbox. The default value of the shortcode is communicated.

The viewer unchecks the checkbox, overriding the default.

If Shortcake built an attribute value of false for the unchecked checkbox state the author's shortcode handler could respond to falsey values as well as truthy values for the generated shortcode.

The existing template behavior requires a string of true to set the truthy state. A shortcode could be similarly expected to respond to a string of false generated by Shortcake to represent the falsey state.

Example shortcode result

[handle do_the_thing="false"]

Example registered shortcode fragment

'attrs' => array(
  array(
    'attr'  => 'do_the_thing',
    'label' => 'Do the thing?',
    'type'  => 'checkbox',
    'value' => 'true',
  ),
)
danielbachhuber commented 9 years ago

Thanks again for the report, @niallkennedy. We'll get this fixed for the next release.

danielbachhuber commented 9 years ago

Holding on this change for v0.5.0, as I don't yet feel 100% confident about this change.

khromov commented 9 years ago

This was a recurring issue with Visual Composer a while back.

There are three states:

Shortcake needs to support all three of these in order for the checkbox to work in a meaningful way.

montchr commented 7 years ago

Yes this makes checkboxes useless in some cases where we need to check for a false value. It's easy to work around with a select field and defining true and false keys but checkboxes are better suited for boolean values.