maddisondesigns / customizer-custom-controls

WordPress Customizer Custom Controls
403 stars 120 forks source link

Image Checkbox control not loading saved values #5

Closed enventa closed 6 years ago

enventa commented 6 years ago

Hi @maddisondesigns!

great collection of controls! Thanks!

I'm integrating the "Image checkbox" custom control. It is working fine except from the fact that it doesn't check saved values when loading.

It is weird, because if I check the code, the function esc_attr( $this->value()) is echoing the saved values into the customize-control-multi-image-checkbox hidden input... but, if I echo that function or the value of $chkboxValues I get nothing, just an empty string. Being that variable empty, it is normal that no option is checked when loading.

Any idea what might be happening? I'm really lost. Can't understand why the saved value can be retrieved within the hidden input but not anywhere else???

Thanks!

maddisondesigns commented 6 years ago

Thanks for the kind words. I've just tested this control again and created a short vid, and everything seems to be working fine.

When I go into the Customizer and check the images that are selected in the Image Checkbox control, and then compare what's in the database, they're the same. When I then change the images that are selected, and save, and then look in the database again, I can see that those values have changed, as expected.

Here's a short vid - https://cl.ly/1C0D0w0L0z2o

You might want to double check that the ID that you've used in add_setting is the same as the one used in add_control

enventa commented 6 years ago

Hey!

I appreciate your video but the issue I'm experiencing has nothing to do with the values not being saved into the database. Values are saved correctly. I can select as many img-checkboxes as I need and then save that without any issue.

The issue is that when loading the Customizer, the setting does not load the saved values... well, it loads them fine in this line of code (number 39 from your control)...

<input type="hidden" id="<?php echo esc_attr( $this->id ); ?>" name="<?php echo esc_attr( $this->id ); ?>" value="<?php echo esc_attr( $this->value() ); ?>" class="customize-control-multi-image-checkbox" <?php $this->link(); ?> />

... but it doesn't retrieved saved values on this other line (number 38, also from your code):

<?php $chkboxValues = explode( ',', esc_attr( $this->value() ) ); ?>

Being so, $chkboxValues is empty, thus no checkbox is marked as "selected" when loading. This is why it is not working, not loading stored values.

Why $this->value() is working on that line of code but not anywhere else??? If I do a print_r($this->value()); I get nothing!

Honestly, I think this might have nothing to do with your controls... I just wanted to know if I can get some help with such a weird thing.

Thanks!

maddisondesigns commented 6 years ago

One of the things I just wanted to show in the vid was that the Control is loading the values fine once you go back into the Customizer.

As I mentioned above, you might want to double check that the ID that you've used in add_setting is the same as the one used in add_control.

Also, I would suggest downloading the code from Github and then doing a diff between my code and yours, in particular, checking that inc/custom-controls.php & inc/customizer.js are both the same and that the code where you're defining your setting & control is similar to mine as well.

Other than that, without actually seeing your code/theme it's hard to say what else could be affecting it. If you have other controls that's you've added, it might be worth commenting them out for the time being and just seeing if this one Image checkbox custom control works. If it does, then one of your other controls might be affecting it for some reason.

enventa commented 6 years ago

After some trial and error without any luck, I ended up rechecking add_panel, add_section, add_setting and add_control functions. It was the settings property for add_control function which was causing the issue. I had something like:

$wp_customize->add_control( new $class( $wp_customize, 'setting_id', array('settings' => array( 'setting_id' ), ... )

Which is wrong because wp_customize_control expects settings to be an array formed by key-value pair, not an array of just values. Documentation is misleading because it says that settings must be an array, but checking the class constructor, you see that it can be just an string with the setting id.

Anyway, nothing to do with your code which is excellent! You can close this issue now. Thanks for your help and suggestions!

maddisondesigns commented 6 years ago

Glad you got it sorted. The Customizer documentation is shit! That's one of the reasons I wrote my own developers guide. In case you haven't seen it, you can find it here:

https://maddisondesigns.com/2017/05/the-wordpress-customizer-a-developers-guide-part-1/ https://maddisondesigns.com/2017/05/the-wordpress-customizer-a-developers-guide-part-2/