钩子文档

customize_register

💡 云策文档标注

概述

customize_register 是 WordPress 3.4 引入的 Theme Customization API 中的动作钩子,用于自定义和管理主题定制器界面。它提供对 WP_Customize_Manager 对象的访问,允许开发者添加、修改设置、部分和控制。

关键要点

  • customize_register 钩子在 WordPress 加载后触发,用于初始化主题定制器。
  • 通过 $wp_customize 对象(WP_Customize_Manager 实例)操作定制器,主要方法包括 add_setting()、add_section()、add_control() 和 get_setting()。
  • 可用于添加文本输入、单选按钮、复选框、选择框、图像上传、文件上传、颜色选择器、页面下拉和分类下拉等多种控件类型。
  • 注意避免使用 is_admin() 条件检查来挂钩 customize_register,否则可能导致自定义部分在定制器中消失。

代码示例

function themename_customize_register($wp_customize){
    $wp_customize->add_section('themename_color_scheme', array(
        'title'    => __('Color Scheme', 'themename'),
        'priority' => 120,
    ));

    $wp_customize->add_setting('themename_theme_options[text_test]', array(
        'default'        => 'value_xyz',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',
    ));

    $wp_customize->add_control('themename_text_test', array(
        'label'      => __('Text Test', 'themename'),
        'section'    => 'themename_color_scheme',
        'settings'   => 'themename_theme_options[text_test]',
    ));
}
add_action('customize_register', 'themename_customize_register');

注意事项

  • 不要将 customize_register 钩子包裹在 is_admin() 条件检查中,这会导致自定义部分在定制器中不可见。
  • 对于现代块主题或全站编辑主题,定制器链接可能默认隐藏,可通过返回 true 到 customize_register 来恢复。
  • 使用 remove_section() 方法可以移除默认部分,如颜色部分。

📄 原文内容

Fires once WordPress has loaded, allowing scripts and styles to be initialized.

Parameters

$managerWP_Customize_Manager

More Information

The ‘customize_register‘ action hook is used to customize and manipulate the Theme Customization admin screen introduced in WordPress Version 3.4. This hook is a component of the Theme Customization API.

This hook gives you access to the $wp_customize object, which is an instance of the WP_Customize_Manager class. It is this class object that controls the Theme Customizer screen.

Generally, there are only 4 methods of the $wp_customize object that you will need to interact with inside the customize_register hook.

WP_Customize_Manager->add_setting()
This adds a new setting to the database.
WP_Customize_Manager->add_section()
This adds a new section (i.e. category/group) to the Theme Customizer page.
WP_Customize_Manager->add_control()
This creates an HTML control that admins can use to change settings. This is also where you choose a section for the control to appear in.
WP_Customize_Manager->get_setting()
This can be used to fetch any existing setting, in the event you need to modify something (like one of WordPress’s default settings).

Example: Customizer with basic controls sample

function themename_customize_register($wp_customize){

$wp_customize->add_section('themename_color_scheme', array(
'title'    => __('Color Scheme', 'themename'),
'description' => '',
'priority' => 120,
));

//  =============================
//  = Text Input                =
//  =============================
$wp_customize->add_setting('themename_theme_options[text_test]', array(
'default'        => 'value_xyz',
'capability'     => 'edit_theme_options',
'type'           => 'option',

));

$wp_customize->add_control('themename_text_test', array(
'label'      => __('Text Test', 'themename'),
'section'    => 'themename_color_scheme',
'settings'   => 'themename_theme_options[text_test]',
));

//  =============================
//  = Radio Input               =
//  =============================
$wp_customize->add_setting('themename_theme_options[color_scheme]', array(
'default'        => 'value2',
'capability'     => 'edit_theme_options',
'type'           => 'option',
));

$wp_customize->add_control('themename_color_scheme', array(
'label'      => __('Color Scheme', 'themename'),
'section'    => 'themename_color_scheme',
'settings'   => 'themename_theme_options[color_scheme]',
'type'       => 'radio',
'choices'    => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
));

//  =============================
//  = Checkbox                  =
//  =============================
$wp_customize->add_setting('themename_theme_options[checkbox_test]', array(
'capability' => 'edit_theme_options',
'type'       => 'option',
));

$wp_customize->add_control('display_header_text', array(
'settings' => 'themename_theme_options[checkbox_test]',
'label'    => __('Display Header Text'),
'section'  => 'themename_color_scheme',
'type'     => 'checkbox',
));

//  =============================
//  = Select Box                =
//  =============================
$wp_customize->add_setting('themename_theme_options[header_select]', array(
'default'        => 'value2',
'capability'     => 'edit_theme_options',
'type'           => 'option',

));
$wp_customize->add_control( 'example_select_box', array(
'settings' => 'themename_theme_options[header_select]',
'label'   => 'Select Something:',
'section' => 'themename_color_scheme',
'type'    => 'select',
'choices'    => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
));

//  =============================
//  = Image Upload              =
//  =============================
$wp_customize->add_setting('themename_theme_options[image_upload_test]', array(
'default'           => 'image.jpg',
'capability'        => 'edit_theme_options',
'type'           => 'option',

));

$wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'image_upload_test', array(
'label'    => __('Image Upload Test', 'themename'),
'section'  => 'themename_color_scheme',
'settings' => 'themename_theme_options[image_upload_test]',
)));

//  =============================
//  = File Upload               =
//  =============================
$wp_customize->add_setting('themename_theme_options[upload_test]', array(
'default'           => 'arse',
'capability'        => 'edit_theme_options',
'type'           => 'option',

));

$wp_customize->add_control( new WP_Customize_Upload_Control($wp_customize, 'upload_test', array(
'label'    => __('Upload Test', 'themename'),
'section'  => 'themename_color_scheme',
'settings' => 'themename_theme_options[upload_test]',
)));

//  =============================
//  = Color Picker              =
//  =============================
$wp_customize->add_setting('themename_theme_options[link_color]', array(
'default'           => '#000',
'sanitize_callback' => 'sanitize_hex_color',
'capability'        => 'edit_theme_options',
'type'           => 'option',

));

$wp_customize->add_control( new WP_Customize_Color_Control($wp_customize, 'link_color', array(
'label'    => __('Link Color', 'themename'),
'section'  => 'themename_color_scheme',
'settings' => 'themename_theme_options[link_color]',
)));

//  =============================
//  = Page Dropdown             =
//  =============================
$wp_customize->add_setting('themename_theme_options[page_test]', array(
'capability'     => 'edit_theme_options',
'type'           => 'option',

));

$wp_customize->add_control('themename_page_test', array(
'label'      => __('Page Test', 'themename'),
'section'    => 'themename_color_scheme',
'type'    => 'dropdown-pages',
'settings'   => 'themename_theme_options[page_test]',
));

// =====================
//  = Category Dropdown =
//  =====================
$categories = get_categories();
$cats = array();
$i = 0;
foreach($categories as $category){
if($i==0){
$default = $category->slug;
$i++;
}
$cats[$category->slug] = $category->name;
}

$wp_customize->add_setting('_s_f_slide_cat', array(
'default'        => $default
));
$wp_customize->add_control( 'cat_select_box', array(
'settings' => '_s_f_slide_cat',
'label'   => 'Select Category:',
'section'  => '_s_f_home_slider',
'type'    => 'select',
'choices' => $cats,
));
}

add_action('customize_register', 'themename_customize_register');

Source

do_action( 'customize_register', $this );

Changelog

Version Description
3.4.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    The “Customize” link will usually disappear if you’re using a modern Block Theme or Full Site Editing theme. You can add it back by returning true to `customize_register`. This can be useful if you’re trying to edit the WooCommerce Store Notice or if you’re using another plugin that places settings in the Customizer.

    add_action( 'customize_register', '__return_true' );

  2. Skip to note 6 content

    Don’t conditionally hook customize_register behind an is_admin() check. e.g.

    // Incorrect:
    if ( is_admin() ) {
        add_action( 'customize_register', 'add_customizer_sections' );
    }

    Doing so will cause your custom sections to disappear from the Customizer.

    // Correct:
    add_action( 'customize_register', 'add_customizer_sections' );