My-Meta-Box icon indicating copy to clipboard operation
My-Meta-Box copied to clipboard

What is the expected behaviour for std values in checkboxes

Open murilopolese opened this issue 10 years ago • 8 comments

First of all, thank you for this class. It is exactly what I was searching for and exactly the way I would like to work with.

I found this question using/studying it: If I set a std value for a checkbox or checkboxlist as "checked", is there a way to uncheck and override this value? I mean, it was suposed to this std be an unchangeable value or just a initial value for fields?

If is an unchangeable value, it could be disabled so user won't get frustrated unchecking and getting the field checked again on page reload. Else if is just an initial value, could be nice to override this default value with saved one.

murilopolese avatar Dec 30 '13 02:12 murilopolese

:+1: It should work as initial value. Unchangeable value does not make any sense :)

ceroz avatar Dec 30 '13 20:12 ceroz

I believe I am seeing the same issue: If for an individual checkbox item the default (std) value is set to checked/true/1/on (anything), it becomes impossible to un-check that checkbox. As soon as the page is saved, it will revert to 'checked'. Essentially, once the default is for that checkbox to be checked, it becomes impossible to un-check it.

To reproduce:

$meta->addCheckbox( $prefix . 'design_show_banner',     array( 'name' => __( 'Show banner',  BACKEND_TEXTDOMAIN ),  'std' => true,  'class' => 'no-fancy') ) );

MarcusWernicke avatar Dec 31 '13 06:12 MarcusWernicke

It works for the reversed logic meaning instead of "un-check to disable feature x" you use "check to enable feature x" but if you all feel that way I'll see what i can do. :)

bainternet avatar Dec 31 '13 07:12 bainternet

"Check to enable feature X" is exactly what my example above does - it displays a banner if that checkbox is checked. It would work just fine if it were not checked by default, which I would like to be the case. Once you let this default to 'checked', the checkbox can no longer be unchecked... so if I want the feature enabled by default, it does not work as expected.

Thanks for looking into this - and Happy New Year!

MarcusWernicke avatar Jan 01 '14 04:01 MarcusWernicke

Once again its working for reversed login, which means in your case it should be "check to disable" but as I said, I'll see what i can do.

bainternet avatar Jan 01 '14 09:01 bainternet

Thank you. I very much appreciate that.

MarcusWernicke avatar Jan 02 '14 03:01 MarcusWernicke

Hi there, I've noticed the same behavior, was wondering if a workaround has been introduced somewhere. Thanks again for the amazing work!

I'm integrating a project that uses 'open (checked)/closed (unchecked)' for the checkbox values, and I realized setting 'std' defaults always leaves boxes checked. If the box is unchecked, the meta_key is dropped, which would be fine unless your code is looking for that 'closed'(unchecked) value. I'm assuming it's just a question of adding conditionals into the save() function of my-meta-box-class.php...did anyone create a workaround for this?

elihubogan avatar May 14 '14 17:05 elihubogan

First of all, great class ! Thank you ! I've found a way to deal with the bug, maybe needs improvement though...

In my-meta-box-class.php, I changed the following lines (put one line in comment and add a couple more) :

public function show() { $this->inGroup = false; global $post;

wp_nonce_field( basename(__FILE__), 'at_meta_box_nonce' );
echo '<table class="form-table">';
foreach ( $this->_fields as $field ) {
  $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false;
  $meta = get_post_meta( $post->ID, $field['id'], !$field['multiple'] );
  // put in comments by sweet7 $meta = ( $meta !== '' ) ? $meta : @$field['std'];
  // ************  begin sweet7 
  if ($field['type'] == 'checkbox') {
    global $post;
    if ($post->post_status == POST_STATUS_AUTO_DRAFT) 
      $meta = $field['std'];
  }
  else
    $meta = ($meta !== '') ? $meta : @$field['std'];
  // ******* end sweet7

...

So, for a checkbox, I verify if the post status is "auto draft" (meaning we are adding a new post) before using the default (std) value.

Seems to work good... Hope It will help...

w7-solutions avatar Aug 20 '15 12:08 w7-solutions