类文档

WP_Customize_Themes_Section

💡 云策文档标注

概述

WP_Customize_Themes_Section 是 WordPress 自定义器中用于主题控件的 UI 容器类,继承自 WP_Customize_Section,专门处理主题相关部分的显示和交互。

关键要点

  • 继承自 WP_Customize_Section,类型为 'themes',用于在自定义器中组织主题控件。
  • 包含属性如 action(定义加载主题类型,如已安装或 WordPress.org 主题)和 filter_type(控制过滤是本地还是远程)。
  • 提供多个方法,如 json() 用于导出参数到 JavaScript,以及 render_template()、filter_bar_content_template() 和 filter_drawer_content_template() 用于渲染 JS 模板。
  • 支持主题搜索、过滤和功能列表显示,增强用户体验。

代码示例

public function json() {
    $exported                = parent::json();
    $exported['action']      = $this->action;
    $exported['filter_type'] = $this->filter_type;

    return $exported;
}

注意事项

  • filter_drawer_content_template() 方法当前使用本地核心功能列表,未来可能改用 WordPress.org API。
  • 确保在自定义器开发中正确设置 action 和 filter_type 属性以控制主题加载行为。

📄 原文内容

Customize Themes Section class.

Description

A UI container for theme controls, which are displayed within sections.

See also

Methods

Name Description
WP_Customize_Themes_Section::filter_bar_content_template Renders the filter bar portion of a themes section as a JS template.
WP_Customize_Themes_Section::filter_drawer_content_template Renders the filter drawer portion of a themes section as a JS template.
WP_Customize_Themes_Section::json Gets section parameters for JS.
WP_Customize_Themes_Section::render Render the themes section, which behaves like a panel.
WP_Customize_Themes_Section::render_template Renders a themes section as a JS template.

Source

class WP_Customize_Themes_Section extends WP_Customize_Section {

/**
* Section type.
*
* @since 4.2.0
* @var string
*/
public $type = 'themes';

/**
* Theme section action.
*
* Defines the type of themes to load (installed, wporg, etc.).
*
* @since 4.9.0
* @var string
*/
public $action = '';

/**
* Theme section filter type.
*
* Determines whether filters are applied to loaded (local) themes or by initiating a new remote query (remote).
* When filtering is local, the initial themes query is not paginated by default.
*
* @since 4.9.0
* @var string
*/
public $filter_type = 'local';

/**
* Gets section parameters for JS.
*
* @since 4.9.0
* @return array Exported parameters.
*/
public function json() {
$exported = parent::json();
$exported['action'] = $this->action;
$exported['filter_type'] = $this->filter_type;

return $exported;
}

/**
* Renders a themes section as a JS template.
*
* The template is only rendered by PHP once, so all actions are prepared at once on the server side.
*
* @since 4.9.0
*/
protected function render_template() {
?>
<li id="accordion-section-{{ data.id }}" class="theme-section">
<button type="button" class="customize-themes-section-title themes-section-{{ data.id }}">{{ data.title }}</button>

<div class="customize-themes-section themes-section-{{ data.id }} control-section-content themes-php">
<div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
<div class="theme-browser rendered">
<div class="customize-preview-header themes-filter-bar">
filter_bar_content_template(); ?>
</div>
filter_drawer_content_template(); ?>
<div class="error unexpected-error" style="display: none; ">
<p>
support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
);
?>
</p>
</div>
<ul class="themes">
</ul>
<p class="no-themes"></p>
<p class="no-themes-local">
%s</button>', __( 'Search WordPress.org themes' ) )
);
?>
</p>
<p class="spinner"></p>
</div>
</div>
</li>
render_template()</a>.
*
* @since 4.9.0
*/
protected function filter_bar_content_template() {
?>
<button type="button" class="button button-primary customize-section-back customize-themes-mobile-back"></button>
<# if ( 'wporg' === data.action ) { #>
<div class="themes-filter-container">
<label for="wp-filter-search-input-{{ data.id }}"></label>
<div class="search-form-input">
<input type="search" id="wp-filter-search-input-{{ data.id }}" aria-describedby="{{ data.id }}-live-search-desc" class="wp-filter-search">
<div class="search-icon" aria-hidden="true"></div>
<span id="{{ data.id }}-live-search-desc" class="screen-reader-text">

</span>
</div>
</div>
<# } else { #>
<div class="themes-filter-container">
<label for="{{ data.id }}-themes-filter"></label>
<div class="search-form-input">
<input type="search" id="{{ data.id }}-themes-filter" aria-describedby="{{ data.id }}-live-search-desc" class="wp-filter-search wp-filter-search-themes" />
<div class="search-icon" aria-hidden="true"></div>
<span id="{{ data.id }}-live-search-desc" class="screen-reader-text">

</span>
</div>
</div>
<# } #>
<div class="filter-themes-wrapper">
<# if ( 'wporg' === data.action ) { #>
<button type="button" class="button feature-filter-toggle">
<span class="filter-count-0"></span><span class="filter-count-filters">
0</span>' );
?>
</span>
</button>
<# } #>
<div class="filter-themes-count">
<span class="themes-displayed">
0</span>' );
?>
</span>
</div>
</div>
render_template()</a>.
*
* @since 4.9.0
*/
protected function filter_drawer_content_template() {
/*
* @todo Use the .org API instead of the local core feature list.
* The .org API is currently outdated and will be reconciled when the .org themes directory is next redesigned.
*/
$feature_list = get_theme_feature_list( false );
?>
<# if ( 'wporg' === data.action ) { #>
<div class="filter-drawer filter-details">
$features ) : ?>
<fieldset class="filter-group">
<legend></legend>
<div class="filter-group-feature">
$feature_name ) : ?>
<input type="checkbox" id="filter-id-<?php echo esc_attr( $feature ); ?>" value="<?php echo esc_attr( $feature ); ?>" />
<label for="filter-id-<?php echo esc_attr( $feature ); ?>"></label>

</div>
</fieldset>

</div>
<# } #>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/customize/class-wp-customize-themes-section.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/customize/class-wp-customize-themes-section.php#L19">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/customize/class-wp-customize-themes-section.php#L19-L208">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/classes/wp_customize_section/">WP_Customize_Section</a><code>wp-includes/class-wp-customize-section.php

Customize Section class.

Changelog

Version Description
4.2.0 Introduced.