WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 17 months ago

#24844 new defect (bug)

get_theme_mods doesn't return the theme customizer preview's new values.

Reported by: nessworthy Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.5.2
Component: Customize Keywords: needs-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Using the theme customizer API, you can call get_theme_mod($option) to return the value for a specific theme option.

In the case of the user interacting with the theme customizer, if they have changed an option, the theme customizer replaces the saved option with the previewed / new setting by hooking onto the theme_mod_$name filter applied in get_theme_mod.

However, should the user need or desire to retrieve all theme mods at once, the filter is not applied, and the new settings are never injected into the saved settings array.

There's a workaround to simply loop through the settings and manually apply the filter, but it would be better if get_theme_mods had an argument like $use_filter and could add the filter in the core functionality.

get_theme_mods is in wp-includes/theme.php on line 746. get_theme_mod is in wp-includes/theme.php on line 776. theme_mod_$name filter is applied in wp-includes/theme.php on lines 780 and 785. theme_mod_$name filter is added in wp-includes/class-wp-customize-setting.php on line 72.

Change History (8)

#1 @SergeyBiryukov
5 years ago

  • Description modified (diff)

#3 @nacin
4 years ago

  • Component changed from Themes to Appearance

#4 @jashwant
4 years ago

Same bug was bugging me.

I can reproduce the issue. Reason is the same, as explained by nessworthy.

I've found a workaround though.

Add your all customizer settings as an array, i.e. keep the id parameter of add_setting as keys of same array.

e.g.

$wp_customize->add_setting('mytheme[header_color]' , array(
    'default' => '#000',
    'sanitize_callback' => 'sanitize_hex_color',
));

$wp_customize->add_setting('mytheme[footer_color]' , array(
    'default' => '#ff0'
));

Now, you can get all customizer settings, in 1 go, with get_theme_mod('mytheme'). This will return an array, which you can use to get values of header_color and footer_color by supplying respective keys.

#5 @celloexpressions
2 years ago

  • Keywords needs-patch added

#25671 and this may be duplicates when it comes down to patching.

#6 @westonruter
2 years ago

#30529 was marked as a duplicate.

This ticket was mentioned in Slack in #core-customize by westonruter. View the logs.


17 months ago

#8 @westonruter
17 months ago

  • Milestone changed from Awaiting Review to Future Release

I think the solution here is to add a filter for the underlying theme_mods_{$theme_slug} option to apply the multidimensional filters at that level instead of at the theme_mod_{$name} level. This will both solve the problem of get_theme_mods not returning the expected values but then the theme_mod_{$name} filters can be removed entirely.

Note: See TracTickets for help on using tickets.