wp_theme_auto_update_setting_template()
云策文档标注
概述
wp_theme_auto_update_setting_template() 函数返回用于显示主题自动更新设置的 JavaScript 模板。该模板包含启用/禁用按钮、状态消息和管理通知,可通过过滤器进行自定义。
关键要点
- 函数返回一个字符串,表示主题自动更新设置的 HTML/JavaScript 模板。
- 模板包含自动更新状态(如“Auto-updates disabled”或“Auto-updates enabled”)和切换按钮。
- 使用 wp_get_admin_notice() 和 wp_get_auto_update_message() 辅助函数来生成通知和消息。
- 可通过 theme_auto_update_setting_template 过滤器钩子修改模板内容。
- 自 WordPress 5.5.0 版本引入。
代码示例
function wp_theme_auto_update_setting_template() {
$notice = wp_get_admin_notice(
'',
array(
'type' => 'error',
'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
)
);
$template = '
<div class="theme-autoupdate">
<div class="theme-autoupdate-actions">
<span class="auto-update-disabled">' . __( 'Auto-updates disabled' ) . '</span>
<span class="auto-update-enabled">' . __( 'Auto-updates enabled' ) . '</span>
<button type="button" class="button-link auto-update-disable">' . __( 'Disable auto-updates' ) . '</button>
<button type="button" class="button-link auto-update-enable">' . __( 'Enable auto-updates' ) . '</button>
</div>
<div class="theme-autoupdate-message">
<span class="auto-update-time"></span>
<span class="auto-update-time-hidden"></span>
' . wp_get_auto_update_message() . '
</div>
' . $notice . '
</div>
';
return apply_filters( 'theme_auto_update_setting_template', $template );
}注意事项
- 模板用于在主题管理界面(如覆盖层)中显示自动更新设置,确保与 wp_prepare_themes_for_js() 的数据对象属性兼容。
- 过滤器 theme_auto_update_setting_template 允许开发者自定义模板字符串,以适应特定需求或样式。
原文内容
Returns the JavaScript template used to display the auto-update setting for a theme.
Source
function wp_theme_auto_update_setting_template() {
$notice = wp_get_admin_notice(
'',
array(
'type' => 'error',
'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
)
);
$template = '
<div class="theme-autoupdate">
<# if ( data.autoupdate.supported ) { #>
<# if ( data.autoupdate.forced === false ) { #>
' . __( 'Auto-updates disabled' ) . '
<# } else if ( data.autoupdate.forced ) { #>
' . __( 'Auto-updates enabled' ) . '
<# } else if ( data.autoupdate.enabled ) { #>
<button type="button" class="toggle-auto-update button-link" data-slug="{{ data.id }}" data-wp-action="disable">
<span class="dashicons dashicons-update spin hidden" aria-hidden="true"></span><span class="label">' . __( 'Disable auto-updates' ) . '</span>
</button>
<# } else { #>
<button type="button" class="toggle-auto-update button-link" data-slug="{{ data.id }}" data-wp-action="enable">
<span class="dashicons dashicons-update spin hidden" aria-hidden="true"></span><span class="label">' . __( 'Enable auto-updates' ) . '</span>
</button>
<# } #>
<# } #>
<# if ( data.hasUpdate ) { #>
<# if ( data.autoupdate.supported && data.autoupdate.enabled ) { #>
<span class="auto-update-time">
<# } else { #>
<span class="auto-update-time hidden">
<# } #>
<br />' . wp_get_auto_update_message() . '</span>
<# } #>
' . $notice . '
</div>
';
/**
* Filters the JavaScript template used to display the auto-update setting for a theme (in the overlay).
*
* See <a href="https://developer.wordpress.org/reference/functions/wp_prepare_themes_for_js/">wp_prepare_themes_for_js()</a> for the properties of the `data` object.
*
* @since 5.5.0
*
* @param string $template The template for displaying the auto-update setting link.
*/
return apply_filters( 'theme_auto_update_setting_template', $template );
}
Hooks
- apply_filters( ‘theme_auto_update_setting_template’, string $template )
-
Filters the JavaScript template used to display the auto-update setting for a theme (in the overlay).
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |