CUSTIZER_REGISTER未将自定义设置保存到数据库

时间:2015-12-11 作者:Christopher Navarre

我目前正在尝试一个基于的WordPress主题Sage. 我所处的阶段是创建自定义控件,以便在WordPress的自定义屏幕上显示,如果您是最终用户,则通常会更改网站标题或主题颜色等内容。

看来我要添加的新设置add_setting 实际上没有创建数据库条目(调用get_theme_mod 在该特定设置上,返回nothing)。我认为这与add\\u控制方法有关,但我检查了WordPress Documentation 就我所知,这和我的想法是一致的。

namespace Roots\\Sage\\Customizer;

use Roots\\Sage\\Assets;

/**
 * Add customization settings for the theme
 */
function customize_register($wp_customize) {
  //////////////////////////
  // WIDGET STYLE OPTIONS //
  //////////////////////////
  $wp_customize->add_section( \'widget_style_section\', array(
     \'title\'  => __(\'Widget Styling\', __NAMESPACE__),
     \'priority\' => 115,
  ));

  $wp_customize->add_setting( \'widget_list_style\', array(
     \'default\' => \'hide\',
     \'transport\' => \'postMessage\',
  ));

  $wp_customize->add_control( \'widget_list_style_control\', array(
      \'label\' => __( \'Display List Style?\', __NAMESPACE__),
      \'Description\' => __(\'Removes the symbols next to bulleted and    unbulleted lists in Widgets\', __NAMESPACE__),
      \'section\' => \'widget_style_section\',
      \'settings\' => \'widget_list_style\',
      \'type\'    => \'radio\', // TODO: CONTROL IS CONFUSED ABOUT WHAT TYPE IT IS, THATS WHY NO REFRESH
      \'choices\' => array(
           \'hide\' => __(\'Hide\'),
           \'show\' => __(\'Show\'),
       ),
  ));

  // Add postMessage support
  $wp_customize->get_setting(\'blogname\')->transport = \'postMessage\';
  $wp_customize->get_setting(\'blogdescription\')->transport = \'postMessage\';

  // TEST - To see if widget_list_style is storing properly
  echo get_theme_mod(\'widget_list_style\');
}

add_action(\'customize_register\', __NAMESPACE__ . \'\\\\customize_register\');

2 个回复
SO网友:Weston Ruter

代码示例的问题是,设置正在使用widget_ 作为前缀。根据customizer handbook:

Important: 不要使用如下设置IDwidget_*, sidebars_widgets[*], nav_menu[*], 或nav_menu_item[*]. 这些设置ID模式分别为小部件实例、侧栏、导航菜单和导航菜单项保留。如果您需要在设置ID中使用“widget”,请将其用作后缀,而不是前缀,例如“homepage_widget”.

以下是按预期工作的修改代码:

<?php

namespace Roots\\Sage\\Customizer;

use Roots\\Sage\\Assets;

/**
 * Add customization settings for the theme
 */
function customize_register($wp_customize) {
    //////////////////////////
    // WIDGET STYLE OPTIONS //
    //////////////////////////
    $wp_customize->add_section( \'style_section_widget\', array(
        \'title\'  => __(\'Widget Styling\', __NAMESPACE__),
        \'priority\' => 115,
    ));

    $wp_customize->add_setting( \'list_style_widget\', array(
        \'default\' => \'hide\',
        \'transport\' => \'postMessage\',
    ));

    $wp_customize->add_control( \'widget_list_style_control\', array(
        \'label\' => __( \'Display List Style?\', __NAMESPACE__),
        \'Description\' => __(\'Removes the symbols next to bulleted and    unbulleted lists in Widgets\', __NAMESPACE__),
        \'section\' => \'style_section_widget\',
        \'settings\' => \'list_style_widget\',
        \'type\'    => \'radio\',
        \'choices\' => array(
            \'hide\' => __(\'Hide\'),
            \'show\' => __(\'Show\'),
        ),
    ));

    // Add postMessage support
    $wp_customize->get_setting(\'blogname\')->transport = \'postMessage\';
    $wp_customize->get_setting(\'blogdescription\')->transport = \'postMessage\';
}

add_action(\'customize_register\', __NAMESPACE__ . \'\\\\customize_register\', 11);
还请注意,我更改了的优先级customize_register 从10(默认值)到11blognameblogdescription 此时可能无法注册设置。

SO网友:AddWeb Solution Pvt Ltd

从…起Theme_Customization_API:
要向自定义程序添加您自己的选项,您至少需要使用2个挂钩:

customize_register
此挂钩允许您定义新的自定义程序面板、节、设置和控件。

wp_head
此挂钩允许您输出自定义生成的CSS,以便您的更改在实时网站上正确显示。

所以你的customize_register 动作如下(将主题名称替换为mytheme):

function mytheme_customize_register( $wp_customize ) {
   //All our sections, settings, and controls will be added here
}
add_action( \'customize_register\', \'mytheme_customize_register\' );
接下来,您需要定义设置,然后定义分区,然后定义控件(控件需要分区和设置才能正常工作)。

Note: 只有在编写自定义Javascript以控制主题定制器的实时预览时,才将其设置为“postMessage”。

相关推荐