dongtanjian9310 2019-04-24 03:09
浏览 59

自定义程序选项在自定义程序屏幕上不返回默认值

This is an unexpected behaviour in the customizer preview, when using 'default' => true with a checkbox.

Use case

I add a checkbox control to the customizer (any page it doesnt matter):

$wp_customize->add_setting( 'my-checkbox', array(
            'type'       => 'option',
            'capability' => 'manage_options',
            'transport'  => 'refresh',
            'default'    => true ///HERE I SET THE DEFAULT "TRUE"
        ) );

$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'my-checkbox', 
       array(
            'label'   => __( 'A checkbox checked by default' ),
            'section' => 'any-page',
            'type'    => 'checkbox'
        ) ) );

i use it anywhere like this:

get_option( 'my-checkbox', true ); //since its TRUE by default, here as well its TRUE

so far its okay, if i open the customizer i see this, using a var_dump(), in this case using it in the home page:

enter image description here

in the homepage without opening the customizer i will get TRUE so thats okay.

But now if i uncheck it in the customizer, i get this:

enter image description here

which is the default from the get_option( 'my-checkbox', true ) this issue its in the customizer-preview the homepage will have the correct value if visited directly.

if i want the value to be FALSE (unchecked), i need to save 2 times, the first time to have the option saved so there is no default value, and the second time to actually save the value of FALSE (unchecked).

Is this a bug in wordpress? debugging it more, i found i am getting the 'default' => true, in the get_option( 'my-checkbox', true ); which is something i set when creating the control, this when using the customizer.

Here i am changing it to another default type, so its more clear:

get_option( 'my-checkbox', 'default' );

in the homepage, i get the correct value:

enter image description here

in the customizer i get TRUE:

enter image description here

which is wrong since in the homepage opening it directly i get default

what can be done? i dont want to pull a global variable to just check if i am in the customizer.

  • 写回答

1条回答 默认 最新

  • dongwei1855 2019-04-24 06:12
    关注

    First when you create code and set default value then that value not set to the database that is why we need to use get_theme_mod function. This function needs to arguments like first the name and second is the default what you want to show. Then if the value saved to the database then the function returns the database value otherwise it will show the default value which you have added in second arguments. I hope this will help you...

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)