2

Why does get_theme_mod return blank (or the default value if specified) but get_option returns the correct (saved) value?

I have a colour picker in the customizer color-primary which functions correctly and saves selected value to the database, however, get_theme_mod returns blank while get_option returns the saved value.

echo get_option('color-primary'); //returns saved value
echo get_theme_mod('color-primary'); // returns blank
echo get_theme_mod('color-primary', '#fafafa'); // returns default value
  • 1
    You mentioned above that you have a colorpicker in the customizer... Are you sure that colorpicker uses theme_mods to save its value and not options? 'Cause if it uses options, it makes perfect sense that using get_theme_mod returns blank while get_option returns the value. Since get_option returns the value, your setting is saved as an option. get_theme_mod will never work for that... – Aristeides Apr 1 '16 at 1:32
  • @Aristeides Thank you! I'd somehow snook a 'type' => 'option' into the code - now works as expected. – mistertaylor Apr 1 '16 at 8:41
1

This is original code from wordpress:

function get_theme_mods()
{
    $theme_slug = get_option('stylesheet');
    $mods = get_option("theme_mods_$theme_slug");
    if (false === $mods)
    {
        $theme_name = get_option('current_theme');
        if (false === $theme_name)
            $theme_name = wp_get_theme()->get('Name');
        $mods = get_option("mods_$theme_name"); // Deprecated location.
        if (is_admin() && false !== $mods)
        {
            update_option("theme_mods_$theme_slug", $mods);
            delete_option("mods_$theme_name");
        }
    }

    return $mods;
}

function get_theme_mods()
{
    $theme_slug = get_option('stylesheet');
    $mods = get_option("theme_mods_$theme_slug");
    if (false === $mods)
    {
        $theme_name = get_option('current_theme');
        if (false === $theme_name)
            $theme_name = wp_get_theme()->get('Name');
        $mods = get_option("mods_$theme_name"); // Deprecated location.
        if (is_admin() && false !== $mods)
        {
            update_option("theme_mods_$theme_slug", $mods);
            delete_option("mods_$theme_name");
        }
    }

    return $mods;
}

As you can see get_theme_mods uses get_option, but it does not use your key that you provided to save your data with, instead it uses your theme name as a key to fetch or save your theme data. Now if you fetch data with get_theme_mod() it will first get_option("theme_mods_$yourthemename") and then it parses the returned value where your saved theme options actually are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.