函数文档

core_auto_updates_settings()

💡 云策文档标注

概述

core_auto_updates_settings() 函数用于显示 WordPress 核心自动更新设置界面,并处理相关状态通知和逻辑判断。它根据配置常量、过滤器和用户操作动态确定更新选项,并输出相应的用户界面和消息。

关键要点

  • 函数显示自动更新设置,包括开发版、次要版本和主要版本的更新选项。
  • 通过检查 WP_AUTO_UPDATE_CORE 常量、WP_Automatic_Updater::is_disabled() 和过滤器(如 allow_major_auto_core_updates)来覆盖 UI 和设置能力。
  • 使用 wp_admin_notice() 显示成功通知,基于 URL 参数 core-major-auto-updates-saved 的值。
  • 输出条件性消息,根据更新状态(如启用主要更新时显示自动更新消息)并提供切换链接。
  • 涉及多个钩子,如 do_action('after_core_auto_updates_settings') 和 apply_filters 用于开发、次要和主要更新。

代码示例

if ( isset( $_GET['core-major-auto-updates-saved'] ) ) {
    if ( 'enabled' === $_GET['core-major-auto-updates-saved'] ) {
        $notice_text = __( 'Automatic updates for all WordPress versions have been enabled. Thank you!' );
        wp_admin_notice(
            $notice_text,
            array(
                'type'        => 'success',
                'dismissible' => true,
            )
        );
    } elseif ( 'disabled' === $_GET['core-major-auto-updates-saved'] ) {
        $notice_text = __( 'WordPress will only receive automatic security and maintenance releases from now on.' );
        wp_admin_notice(
            $notice_text,
            array(
                'type'        => 'success',
                'dismissible' => true,
            )
        );
    }
}

注意事项

  • UI 可能被 WP_AUTO_UPDATE_CORE 常量、AUTOMATIC_UPDATER_DISABLED 或过滤器覆盖,导致 $can_set_update_option 为 false。
  • 使用前需确保相关文件(如 class-wp-upgrader.php)已加载,并注意版本控制或文件修改权限可能禁用自动更新。

📄 原文内容

Display WordPress auto-updates settings.

Source

function core_auto_updates_settings() {
if ( isset( $_GET['core-major-auto-updates-saved'] ) ) {
if ( 'enabled' === $_GET['core-major-auto-updates-saved'] ) {
$notice_text = __( 'Automatic updates for all WordPress versions have been enabled. Thank you!' );
wp_admin_notice(
$notice_text,
array(
'type' => 'success',
'dismissible' => true,
)
);
} elseif ( 'disabled' === $_GET['core-major-auto-updates-saved'] ) {
$notice_text = __( 'WordPress will only receive automatic security and maintenance releases from now on.' );
wp_admin_notice(
$notice_text,
array(
'type' => 'success',
'dismissible' => true,
)
);
}
}

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$updater = new WP_Automatic_Updater();

// Defaults:
$upgrade_dev = get_site_option( 'auto_update_core_dev', 'enabled' ) === 'enabled';
$upgrade_minor = get_site_option( 'auto_update_core_minor', 'enabled' ) === 'enabled';
$upgrade_major = get_site_option( 'auto_update_core_major', 'unset' ) === 'enabled';

$can_set_update_option = true;
// WP_AUTO_UPDATE_CORE = true (all), 'beta', 'rc', 'development', 'branch-development', 'minor', false.
if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
if ( false === WP_AUTO_UPDATE_CORE ) {
// Defaults to turned off, unless a filter allows it.
$upgrade_dev = false;
$upgrade_minor = false;
$upgrade_major = false;
} elseif ( true === WP_AUTO_UPDATE_CORE
|| in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true )
) {
// ALL updates for core.
$upgrade_dev = true;
$upgrade_minor = true;
$upgrade_major = true;
} elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
// Only minor updates for core.
$upgrade_dev = false;
$upgrade_minor = true;
$upgrade_major = false;
}

// The UI is overridden by the `WP_AUTO_UPDATE_CORE` constant.
$can_set_update_option = false;
}

if ( $updater->is_disabled() ) {
$upgrade_dev = false;
$upgrade_minor = false;
$upgrade_major = false;

/*
* The UI is overridden by the `AUTOMATIC_UPDATER_DISABLED` constant
* or the `automatic_updater_disabled` filter,
* or by `wp_is_file_mod_allowed( 'automatic_updater' )`.
* See `WP_Automatic_Updater::is_disabled()`.
*/

$can_set_update_option = false;
}

// Is the UI overridden by a plugin using the `allow_major_auto_core_updates` filter?
if ( has_filter( 'allow_major_auto_core_updates' ) ) {
$can_set_update_option = false;
}

/** This filter is documented in wp-admin/includes/class-core-upgrader.php */
$upgrade_dev = apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev );
/** This filter is documented in wp-admin/includes/class-core-upgrader.php */
$upgrade_minor = apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
/** This filter is documented in wp-admin/includes/class-core-upgrader.php */
$upgrade_major = apply_filters( 'allow_major_auto_core_updates', $upgrade_major );

$auto_update_settings = array(
'dev' => $upgrade_dev,
'minor' => $upgrade_minor,
'major' => $upgrade_major,
);

if ( $upgrade_major ) {
$wp_version = wp_get_wp_version();
$updates = get_core_updates();

if ( isset( $updates[0]->version ) && version_compare( $updates[0]->version, $wp_version, '>' ) ) {
echo '<p>' . wp_get_auto_update_message() . '</p>';
}
}

$action_url = self_admin_url( 'update-core.php?action=core-major-auto-updates-settings' );
?>

<p class="auto-update-status">
is_vcs_checkout( ABSPATH ) ) {
_e( 'This site appears to be under version control. Automatic updates are disabled.' );
} elseif ( $upgrade_major ) {
_e( 'This site is automatically kept up to date with each new version of WordPress.' );

if ( $can_set_update_option ) {
echo '<br />';
printf(
'<a href="%s" class="core-auto-update-settings-link core-auto-update-settings-link-disable">%s</a>',
wp_nonce_url( add_query_arg( 'value', 'disable', $action_url ), 'core-major-auto-updates-nonce' ),
__( 'Switch to automatic updates for maintenance and security releases only.' )
);
}
} elseif ( $upgrade_minor ) {
_e( 'This site is automatically kept up to date with maintenance and security releases of WordPress only.' );

if ( $can_set_update_option ) {
echo '<br />';
printf(
'<a href="%s" class="core-auto-update-settings-link core-auto-update-settings-link-enable">%s</a>',
wp_nonce_url( add_query_arg( 'value', 'enable', $action_url ), 'core-major-auto-updates-nonce' ),
__( 'Enable automatic updates for all new versions of WordPress.' )
);
}
} else {
_e( 'This site will not receive automatic updates for new versions of WordPress.' );
}
?>
</p>

</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#L306">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/update-core.php#L306-L455">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/after_core_auto_updates_settings/"><span class="hook-func">do_action</span>( ‘after_core_auto_updates_settings’, <nobr><span class="arg-type">array</span> <span class="arg-name">$auto_update_settings</span></nobr> )</a></dt><dd><p>Fires after the major core auto-update settings.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/allow_dev_auto_core_updates/"><span class="hook-func">apply_filters</span>( ‘allow_dev_auto_core_updates’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$upgrade_dev</span></nobr> )</a></dt><dd><p>Filters whether to enable automatic core updates for development versions.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/allow_major_auto_core_updates/"><span class="hook-func">apply_filters</span>( ‘allow_major_auto_core_updates’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$upgrade_major</span></nobr> )</a></dt><dd><p>Filters whether to enable major automatic core updates.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/allow_minor_auto_core_updates/"><span class="hook-func">apply_filters</span>( ‘allow_minor_auto_core_updates’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$upgrade_minor</span></nobr> )</a></dt><dd><p>Filters whether to enable minor automatic core updates.</p>
</dd></dl></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_get_wp_version/">wp_get_wp_version()</a><code>wp-includes/functions.php

Returns the current WordPress version.

wp_admin_notice()wp-includes/functions.php

Outputs an admin notice.

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

Determines the appropriate auto-update message to be displayed.

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

Gets available core updates.

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.

has_filter()wp-includes/plugin.php

Checks if any filter has been registered for a hook.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

wp_nonce_url()wp-includes/functions.php

Retrieves URL with nonce added to URL query.

add_query_arg()wp-includes/functions.php

Retrieves a modified URL query string.

apply_filters()wp-includes/plugin.php

Calls the callback functions that have been added to a filter hook.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

get_site_option()wp-includes/option.php

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

Show 8 moreShow less

Changelog

Version Description
5.6.0 Introduced.