函数文档

wp_update_site()

💡 云策文档标注

概述

wp_update_site() 函数用于更新数据库中的站点信息。它接收站点ID和更新数据数组作为参数,执行验证和数据库操作,并返回更新后的站点ID或WP_Error对象。

关键要点

  • 参数:$site_id(必需,站点ID)和$data(必需,更新数据数组,支持键参考wp_insert_site())。
  • 返回值:成功时返回更新站点的ID(整数),失败时返回WP_Error对象。
  • 函数流程:验证站点ID和存在性,准备数据,更新数据库表,清理缓存,触发wp_update_site钩子。
  • 相关函数:包括wp_prepare_site_data()、get_site()、clean_blog_cache()等。
  • 钩子:do_action('wp_update_site', $new_site, $old_site)在站点更新后触发。

代码示例

function wp_update_site( $site_id, array $data ) {
    global $wpdb;

    if ( empty( $site_id ) ) {
        return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
    }

    $old_site = get_site( $site_id );
    if ( ! $old_site ) {
        return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
    }

    $defaults                 = $old_site->to_array();
    $defaults['network_id']   = (int) $defaults['site_id'];
    $defaults['last_updated'] = current_time( 'mysql', true );
    unset( $defaults['blog_id'], $defaults['site_id'] );

    $data = wp_prepare_site_data( $data, $defaults, $old_site );
    if ( is_wp_error( $data ) ) {
        return $data;
    }

    if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
        return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
    }

    clean_blog_cache( $old_site );

    $new_site = get_site( $old_site->id );

    do_action( 'wp_update_site', $new_site, $old_site );

    return (int) $new_site->id;
}

注意事项

  • 确保$site_id不为空且站点存在,否则返回WP_Error。
  • 更新数据需符合wp_prepare_site_data()的要求,否则可能返回错误。
  • 函数内部使用$wpdb->update()操作数据库,失败时返回WP_Error。
  • 更新后自动清理缓存并触发wp_update_site钩子,便于开发者扩展功能。
  • 自WordPress 5.1.0版本引入。

📄 原文内容

Updates a site in the database.

Parameters

$site_idintrequired
ID of the site that should be updated.
$dataarrayrequired
Site data to update. See wp_insert_site() for the list of supported keys.

Return

int|WP_Error The updated site’s ID on success, or error object on failure.

Source

function wp_update_site( $site_id, array $data ) {
	global $wpdb;

	if ( empty( $site_id ) ) {
		return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
	}

	$old_site = get_site( $site_id );
	if ( ! $old_site ) {
		return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
	}

	$defaults                 = $old_site->to_array();
	$defaults['network_id']   = (int) $defaults['site_id'];
	$defaults['last_updated'] = current_time( 'mysql', true );
	unset( $defaults['blog_id'], $defaults['site_id'] );

	$data = wp_prepare_site_data( $data, $defaults, $old_site );
	if ( is_wp_error( $data ) ) {
		return $data;
	}

	if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
		return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
	}

	clean_blog_cache( $old_site );

	$new_site = get_site( $old_site->id );

	/**
	 * Fires once a site has been updated in the database.
	 *
	 * @since 5.1.0
	 *
	 * @param WP_Site $new_site New site object.
	 * @param WP_Site $old_site Old site object.
	 */
	do_action( 'wp_update_site', $new_site, $old_site );

	return (int) $new_site->id;
}

Hooks

do_action( ‘wp_update_site’, WP_Site $new_site, WP_Site $old_site )

Fires once a site has been updated in the database.

Changelog

Version Description
5.1.0 Introduced.