admin管理员组文章数量:1122832
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.
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.
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.
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.
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.
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.
Share Improve this question edited Jul 20, 2018 at 10:07 SVE 1035 bronze badges asked Dec 20, 2016 at 21:54 user3638239user3638239 1532 silver badges9 bronze badges 5- I updated my answer but could not dupe the problem you marked under 1) @user3638239 – prosti Commented Dec 30, 2016 at 18:05
- How many settings you have? Do they have the name collisions? – prosti Commented 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! – user3638239 Commented Jan 2, 2017 at 21:29
- @user3638239 Did you ever find the solution for this. I'm facing exact same thing. – Shaheed ulHaq Commented 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. – user3638239 Commented Nov 21, 2017 at 19:39
1 Answer
Reset to default 0It 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.
本文标签: theme customizerGetthememod not retrieving value
版权声明:本文标题:theme customizer - Get_theme_mod not retrieving value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283545a1927003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论