函数文档

wp_schedule_update_user_counts()

💡 云策文档标注

概述

wp_schedule_update_user_counts() 函数用于安排一个定期重新计算用户总数的任务。它仅在主站点且未处于安装模式时,通过 wp_schedule_event() 设置一个每日两次的 cron 事件。

关键要点

  • 函数 wp_schedule_update_user_counts() 用于调度用户总数的定期更新。
  • 仅在主站点(通过 is_main_site() 检查)且未处于安装模式(通过 wp_installing() 检查)时执行。
  • 使用 wp_schedule_event() 安排一个每日两次('twicedaily')的 cron 事件,钩子为 'wp_update_user_counts'。
  • 通过 wp_next_scheduled() 检查是否已有调度,避免重复安排。
  • 自 WordPress 6.0.0 版本引入。

代码示例

function wp_schedule_update_user_counts() {
    if ( ! is_main_site() ) {
        return;
    }

    if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) {
        wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' );
    }
}

注意事项

  • 此函数仅在多站点网络的主站点中有效,非主站点会直接返回。
  • 确保在调用前检查 WordPress 是否处于安装模式,以避免在安装过程中调度事件。
  • 使用 'twicedaily' 间隔,具体执行时间取决于 WordPress cron 系统的配置。

📄 原文内容

Schedules a recurring recalculation of the total count of users.

Source

function wp_schedule_update_user_counts() {
	if ( ! is_main_site() ) {
		return;
	}

	if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) {
		wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' );
	}
}

Changelog

Version Description
6.0.0 Introduced.