Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the expected behaviour for std values in checkboxes #102

Open
murilopolese opened this issue Dec 30, 2013 · 8 comments
Open

What is the expected behaviour for std values in checkboxes #102

murilopolese opened this issue Dec 30, 2013 · 8 comments

Comments

@murilopolese
Copy link

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.

@ceroz
Copy link

ceroz commented Dec 30, 2013

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

@MarcusWernicke
Copy link

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') ) );

@bainternet
Copy link
Owner

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. :)

@MarcusWernicke
Copy link

"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!

@bainternet
Copy link
Owner

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.

@MarcusWernicke
Copy link

Thank you. I very much appreciate that.

@elihubogan
Copy link

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?

@w7-solutions
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants