函数文档

list_theme_updates()

💡 云策文档标注

概述

list_theme_updates() 函数用于在 WordPress 后台显示主题升级表单,检查主题更新并处理兼容性信息。它生成一个包含主题列表、更新状态和操作按钮的界面,供用户批量升级主题。

关键要点

  • 函数首先调用 get_theme_updates() 获取有更新的主题列表,若无更新则显示提示信息。
  • 通过循环遍历主题,检查每个主题的 WordPress 和 PHP 版本兼容性,并显示相应的警告或建议。
  • 输出一个表单,包含复选框选择主题、非ce字段和提交按钮,用于执行主题升级操作。
  • 在表单顶部显示注意事项,提醒用户自定义修改可能丢失,建议使用子主题。

代码示例

// 示例代码片段展示主题兼容性检查逻辑
$requires_wp  = isset( $theme->update['requires'] ) ? $theme->update['requires'] : null;
$requires_php = isset( $theme->update['requires_php'] ) ? $theme->update['requires_php'] : null;

$compatible_wp  = is_wp_version_compatible( $requires_wp );
$compatible_php = is_php_version_compatible( $requires_php );

if ( ! $compatible_wp && ! $compatible_php ) {
    $compat .= '<p class="notice notice-error">' . __( 'This update does not work with your versions of WordPress and PHP.' ) . '</p>';
}

注意事项

  • 主题升级会覆盖自定义文件,建议使用子主题进行修改以避免数据丢失。
  • 兼容性检查依赖于 is_wp_version_compatible() 和 is_php_version_compatible() 函数,需确保当前环境满足要求。
  • 表单使用 wp_nonce_field() 添加安全 nonce 字段,防止 CSRF 攻击。

📄 原文内容

Display the upgrade themes form.

Source

function list_theme_updates() {
$themes = get_theme_updates();
if ( empty( $themes ) ) {
echo '<h2>' . __( 'Themes' ) . '</h2>';
echo '<p>' . __( 'Your themes are all up to date.' ) . '</p>';
return;
}

$form_action = 'update-core.php?action=do-theme-upgrade';

$themes_count = count( $themes );
?>
<h2>
(%d)</span>',
__( 'Themes' ),
number_format_i18n( $themes_count )
);
?>
</h2>
<p></p>
<p>
Please Note:</strong> Any customizations you have made to theme files will be lost. Please consider using <a href="%s">child themes</a> for modifications.' ),
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' )
);
?>
</p>
<form method="post" action="<?php echo esc_url( $form_action ); ?>" name="upgrade-themes" class="upgrade">

<p><input id="upgrade-themes" class="button" type="submit" value="<?php esc_attr_e( 'Update Themes' ); ?>" name="upgrade" /></p>
<table class="widefat updates-table" id="update-themes-table">
<thead>
<tr>
<td class="manage-column check-column"><input type="checkbox" id="themes-select-all" /></td>
<td class="manage-column"><label for="themes-select-all"></label></td>
</tr>
</thead>

<tbody class="plugins">
$theme ) {
$requires_wp = isset( $theme->update['requires'] ) ? $theme->update['requires'] : null;
$requires_php = isset( $theme->update['requires_php'] ) ? $theme->update['requires_php'] : null;

$compatible_wp = is_wp_version_compatible( $requires_wp );
$compatible_php = is_php_version_compatible( $requires_php );

$compat = '';

if ( ! $compatible_wp && ! $compatible_php ) {
$compat .= '<br />' . __( 'This update does not work with your versions of WordPress and PHP.' ) . ' ';
if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
$compat .= sprintf(
/* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
__( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
esc_url( self_admin_url( 'update-core.php' ) ),
esc_url( wp_get_update_php_url() )
);

$annotation = wp_get_update_php_annotation();

if ( $annotation ) {
$compat .= '</p><p><em>' . $annotation . '</em>';
}
} elseif ( current_user_can( 'update_core' ) ) {
$compat .= sprintf(
/* translators: %s: URL to WordPress Updates screen. */
__( '<a href="%s">Please update WordPress</a>.' ),
esc_url( self_admin_url( 'update-core.php' ) )
);
} elseif ( current_user_can( 'update_php' ) ) {
$compat .= sprintf(
/* translators: %s: URL to Update PHP page. */
__( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);

$annotation = wp_get_update_php_annotation();

if ( $annotation ) {
$compat .= '</p><p><em>' . $annotation . '</em>';
}
}
} elseif ( ! $compatible_wp ) {
$compat .= '<br />' . __( 'This update does not work with your version of WordPress.' ) . ' ';
if ( current_user_can( 'update_core' ) ) {
$compat .= sprintf(
/* translators: %s: URL to WordPress Updates screen. */
__( '<a href="%s">Please update WordPress</a>.' ),
esc_url( self_admin_url( 'update-core.php' ) )
);
}
} elseif ( ! $compatible_php ) {
$compat .= '<br />' . __( 'This update does not work with your version of PHP.' ) . ' ';
if ( current_user_can( 'update_php' ) ) {
$compat .= sprintf(
/* translators: %s: URL to Update PHP page. */
__( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);

$annotation = wp_get_update_php_annotation();

if ( $annotation ) {
$compat .= '</p><p><em>' . $annotation . '</em>';
}
}
}

$checkbox_id = 'checkbox_' . md5( $theme->get( 'Name' ) );
?>
<tr>
<td class="check-column">

<input type="checkbox" name="checked[]" id="<?php echo $checkbox_id; ?>" value="<?php echo esc_attr( $stylesheet ); ?>" />
<label for="<?php echo $checkbox_id; ?>">
<span class="screen-reader-text">
display( 'Name' ) );
?>
</span>
</label>

</td>
<td class="plugin-title"><p>
<img src="<?php echo esc_url( $theme->get_screenshot() . '?ver=' . $theme->version ); ?>" width="85" height="64" class="updates-table-screenshot" alt="" />
<strong>display( 'Name' ); ?></strong>
display( 'Version' ),
$theme->update['new_version']
);

echo ' ' . $compat;

if ( in_array( $stylesheet, $auto_updates, true ) ) {
echo $auto_update_notice;
}
?>
</p></td>
</tr>

</tbody>

<tfoot>
<tr>
<td class="manage-column check-column"><input type="checkbox" id="themes-select-all-2" /></td>
<td class="manage-column"><label for="themes-select-all-2"></label></td>
</tr>
</tfoot>
</table>
<p><input id="upgrade-themes-2" class="button" type="submit" value="<?php esc_attr_e( 'Update Themes' ); ?>" name="upgrade" /></p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/update-core.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/update-core.php#L639">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/update-core.php#L639-L808">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/functions/wp_is_auto_update_enabled_for_type/">wp_is_auto_update_enabled_for_type()</a><code>wp-admin/includes/update.php

Checks whether auto-updates are enabled.

wp_get_auto_update_message()wp-admin/includes/update.php

Determines the appropriate auto-update message to be displayed.

is_wp_version_compatible()wp-includes/functions.php

Checks compatibility with the current WordPress version.

is_php_version_compatible()wp-includes/functions.php

Checks compatibility with the current PHP version.

wp_get_update_php_annotation()wp-includes/functions.php

Returns the default annotation for the web hosting altering the “Update PHP” page URL.

wp_get_update_php_url()wp-includes/functions.php

Gets the URL to learn more about updating the PHP version the site is running on.

get_theme_updates()wp-admin/includes/update.php

Retrieves themes with updates available.

esc_attr_e()wp-includes/l10n.php

Displays translated text that has been escaped for safe use in an attribute.

wp_nonce_field()wp-includes/functions.php

Retrieves or display nonce hidden field for forms.

self_admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for either the current site or the network depending on context.

current_user_can()wp-includes/capabilities.php

Returns whether the current user has the specified capability.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

number_format_i18n()wp-includes/functions.php

Converts float number to format based on the locale.

get_site_option()wp-includes/option.php

Retrieve an option value for the current network based on name of option.

Show 12 moreShow less

Changelog

Version Description
2.9.0 Introduced.