3

I'm using the Wordpress customizer feature to configure my theme and I constantly bumping into the same annoying issue, sometimes I don't get any value from get_theme_mod while I see the effect of my change in the preview window.

Here is some code:

    $active_type="home";
    $wp_customize->add_setting($active_type.'_categories_max',array(
            'default'      => '10',
            'transport'    => 'refresh'
            ));

    $wp_customize->add_control( $active_type.'_categories_max',array(
            'type' => 'select',
            'label' => 'Max Items',
            'section' => $active_type.'_categories_section',
            'choices' => array(
                    '5' => '2',
                    '10' => '10',
                    '15' => '15',
                    '20' => '20',
                ),
            )); 

on my php page, I have:

$test = get_theme_mod('home_categories_max');
echo $test;

When I'm in the customizer, each time I change the value, I do see the correct value in the preview window. If I save and then visit the page directly (without the customizer), I don't get the value all the time. Sometimes I get immediately the value, sometimes I don't get it at all without changing anything to the code.

I first thought that it was a caching issue but emptying cache on server and browser did not solve the issue. If I save the value, exit the customizer then come back, I see that the value has been saved but get_theme_mod still returns an empty value.

I really don't see what's wrong with my code as it sometimes work without changing anything.

Do you have any idea?

Thanks

Laurent

UPDATE 28/12:

I see two issues happening.

  1. Not possible to save When I hit the save & publish button, the "busy" icon appears, then disappears but the save & publish button remains unchanged. If I check in the database, nothing has been updated.

  2. Saved in db but not visible When I see the right entry in the database like this one:

    {
      "2016-Foundation6-v0.6::single_categories_location_priority": {
        "value": "0",
        "type": "theme_mod",
        "user_id": 1
      }
    }
    

I sometimes get no value when using get_theme_mod

Another thing

I noticed that my success rate was higher when I do only one change. Let's say I go to the customizer for the first time, I change one thing and save, I get this:

{"success":true,"data":{"setting_validities":{"single_carousel_location":true},"changeset_status":"publish"
,"next_changeset_uuid":"9aff0388-b0bc-4be8-ba1a-2fd3cf24772c"}}

If I try to make other changes (not related to the first one), I get this kind of message:

{"success":false,"data":{"message":"","code":"changeset_already_published","data":null}}

If I leave again, go back to the customizer and make one change and save, it usually works

I haven't changed a line of code in the whole process.

5
  • I updated my answer but could not dupe the problem you marked under 1) @user3638239
    – prosti
    Dec 30, 2016 at 18:05
  • How many settings you have? Do they have the name collisions?
    – prosti
    Dec 30, 2016 at 18:49
  • I checked names and there are no duplicates, there are more than 100 possible settings. I think the only solution would be to deactivate them one by one until I find something. Thanks! Jan 2, 2017 at 21:29
  • @user3638239 Did you ever find the solution for this. I'm facing exact same thing. Oct 20, 2017 at 11:43
  • yes, I was using a script calculating the rendering speed for my scripts and when I switched it off, it solved the issue. Nov 21, 2017 at 19:39

1 Answer 1

0

It is really simple. set_theme_mod

File: /wp-includes/theme.php
889: function set_theme_mod( $name, $value ) {
890:    $mods = get_theme_mods();
891:    $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
892: 
893:    /**
894:     * Filters the theme mod value on save.
895:     *
896:     * The dynamic portion of the hook name, `$name`, refers to the key name of
897:     * the modification array. For example, 'header_textcolor', 'header_image',
898:     * and so on depending on the theme options.
899:     *
900:     * @since 3.9.0
901:     *
902:     * @param string $value     The new value of the theme mod.
903:     * @param string $old_value The current value of the theme mod.
904:     */
905:    $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value );
906: 
907:    $theme = get_option( 'stylesheet' );
908:    update_option( "theme_mods_$theme", $mods );
909: }

will save into the options table theme_mods_$theme where $theme is your theme name. But this will not happen until you press the Save & Publish Customizer button.

The preview page is like a dream land (Lat: Et est fallacia lunae). Whatever you do the values will not be saved to the database. And I think this is the only issue you are facing.


I duped what you have on your end. I can confirm I can save the values every time.

function _20161230_customize_register( $wp_customize ) {

    $active_type="home";

    $wp_customize->add_section( $active_type. 'just_listed' , array(
            'title'      => __( 'Just Listed Settings', 'microformata' ),
            'priority'   => 53,
    ) );  

    $wp_customize->add_setting($active_type.'_categories_max',array(
            'default'      => '10',
            'transport'    => 'refresh'
            ));

    $wp_customize->add_control( $active_type.'_categories_max',array(
            'type' => 'select',
            'label' => 'Max Items',
            'section' => $active_type.'just_listed',
            'choices' => array(
                    '5' => '2',
                    '10' => '10',
                    '15' => '15',
                    '20' => '20',
                ),
            )); 
}

add_action( 'customize_register', '_20161230_customize_register' );

The Ajax result will be like this:

{success: true, data: {setting_validities: {home_categories_max: true}, changeset_status: "publish",…}}
data
:
{setting_validities: {home_categories_max: true}, changeset_status: "publish",…}
changeset_status
:
"publish"
next_changeset_uuid
:
"4b1ac702-3ae2-422d-875a-a02b6f5b2d65"
setting_validities
:
{home_categories_max: true}
success
:
true

What I am not sure, can you disable plugins you use and then test. I know you use 4.7, because of the error message you got.

8
  • I'm sorry but that's not the issue. I know that I have to click on save & publish but sometimes I can't access the value with get_theme_mod while the updated value is in the database. Hitting the save & publish button multiple times doesn't help. Dec 24, 2016 at 17:31
  • if it is in a database, get theme_mod should. How do you checked it is in the database?
    – prosti
    Dec 24, 2016 at 17:37
  • I read the content of the corresponding table by using phpmyadmin Dec 26, 2016 at 18:21
  • And what can you confirm from the PHPMyAdmin? Have you read the serialized data from the options table theme_mods_$theme?
    – prosti
    Dec 27, 2016 at 12:22
  • { "2016-Foundation6-v0.6::single_categories_location_priority": { "value": "0", "type": "theme_mod", "user_id": 1 } } Dec 28, 2016 at 18:44

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.