WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#26817 closed enhancement (invalid)

WP_Customize_Setting default value set to empty string by default

Reported by: bassjobsen Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Customize Keywords:
Focuses: Cc:

Description

class-wp-customize-setting.php

Sets $default to an empty string. I think this should be null or false.

This empty string requires to set the value of default explicit to false always when use customizer's add_setting(). In the cases you don't want to set the default value.

Example use case:

I want to create a customizable color setting and in my code i will set an alternative code if color is not set.

So i use $wp_customize->add_setting('color',array()), although it doesn't throw an errors, docs tell me the setting array is required? (see: http://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_setting).

Now in my code i try to use:
get_theme_option('color','green');

Note i expect my color become green unless the user sets it to some different value via the customizer.

The above won't work cause get_theme_option('color','green'); returns an empty string instead of green.

The above can also be solved $wp_customize->add_setting('color',array('default'=>false)) but defining a default as false if i don't need a default doesn't feel solid.

Change History (8)

#1 @SergeyBiryukov
6 years ago

  • Component changed from Database to Appearance

#2 follow-up: @obenland
6 years ago

In your example the default value in the Customizer should be green, rather than an empty string or false.

In cases where the theme mod is not set, get_theme_mod() would fall back to green passed as a second argument, in all other cases it would be set by the default value in the Customizer.

#3 in reply to: ↑ 2 ; follow-up: @bassjobsen
6 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Replying to obenland:

In your example the default value in the Customizer should be green, rather than an empty string or false.

In cases where the theme mod is not set, get_theme_mod() would fall back to green passed as a second argument, in all other cases it would be set by the default value in the Customizer.

Okay, thanks. I think it makes sense. So in add_setting() the second paramter $args is required and has a required key (default).

#4 in reply to: ↑ 3 @obenland
6 years ago

Replying to bassjobsen:

So in add_setting() the second paramter $args is required and has a required key (default).

add_setting() is defined here. The first argument is $id, which is required (nothing assigned to it), the second argument, $args, is set to be an empty array if nothing gets passed. So no, $args is not required.

Since the default here is an empty array, we can assume that it doesn't have a required key either. Looking at the constructor in WP_Customize_Setting, we see that it gets all default properties of that class and loops through the $args array on the hunt for new values. Since all properties have a defined value (including $default, which is an empty string), we now know that there is no required key necessary.

#5 @obenland
6 years ago

  • Milestone Awaiting Review deleted

#6 follow-up: @bassjobsen
6 years ago

Thanks for your explanation. With this explanation i think my original problem still exists. When not setting any value (or call with an empty array) i don't expect the default become set to an empty string. And then i also expected get_theme_option('color','green'); returns green and not an empty string.

#7 in reply to: ↑ 6 @obenland
6 years ago

If you expect anything other than the default value for $default, you should probably set it to what you need it to be, in this case 'green'.

Note: See TracTickets for help on using tickets.