Change setting name in Customizer and keep the data

I created a theme while back when I was new to Customizer API and for some weird reasons I named theme settings as:

$wp_customize->add_setting('thefunk_theme_options[header_color]

Instead of just header_color and saved every setting in an array like thefunk_theme_options[header_color]

So I recently found out that it’s generating some issues, so I want to save every setting, let’s say, thefunk_theme_options[header_color] to header_color or thefunk_header_color.

Now I know how I can change the settings in the Customizer. However, it will affect everyone who is currently using the theme. All the customization will be gone, so does any one know how I can import the data from the old tables and add it to the new tables?

1 Answer
1

Personally I think keeping your mods in an array is a nice solution, but transferring them shouldn’t be too difficult. Just loop through the array like this:

$all_mods = get_theme_mod('thefunk_theme_options'); // retrieve array
foreach ($all_mods as $key -> $value) {
    if (!empty($value)) set_theme_mod ($key,$value); // set array element as separate mod
    } 

Since you have to do this only once, when the updated theme is ran first, you may want to insert an extra check upfront and delete the array afterwards.

Leave a Comment