函数文档

insert_blog()

💡 云策文档标注

概述

insert_blog() 函数用于在 WordPress 多站点环境中,向 wp_blogs 表存储新站点的基本信息,并返回新站点的 ID。该函数自 WordPress 5.1.0 起已被弃用,建议使用 wp_insert_site() 替代。

关键要点

  • 函数功能:在 wp_blogs 表中创建新行,返回新站点的 ID,是创建新站点的第一步。
  • 弃用状态:自 WordPress 5.1.0 起弃用,推荐使用 wp_insert_site() 函数。
  • 参数要求:接受三个必需参数:$domain(新站点的域名)、$path(新站点的路径)和 $site_id(站点 ID,通常设置为 1)。
  • 返回值:成功时返回新站点的 ID,失败时返回 false。
  • 相关函数:内部调用 wp_insert_site() 和 clean_blog_cache(),并标记为弃用。

代码示例

function insert_blog($domain, $path, $site_id) {
    _deprecated_function( __FUNCTION__, '5.1.0', 'wp_insert_site()' );

    $data = array(
        'domain'  => $domain,
        'path'    => $path,
        'site_id' => $site_id,
    );

    $site_id = wp_insert_site( $data );
    if ( is_wp_error( $site_id ) ) {
        return false;
    }

    clean_blog_cache( $site_id );

    return $site_id;
}

注意事项

  • 该函数已弃用,新代码应使用 wp_insert_site() 以避免兼容性问题。
  • 在非多网络安装中,$site_id 参数通常应设置为 1。
  • 函数内部处理错误,如果 wp_insert_site() 返回 WP_Error,则返回 false。

📄 原文内容

Store basic site info in the blogs table.

Description

This function creates a row in the wp_blogs table and returns the new blog’s ID. It is the first step in creating a new blog.

See also

Parameters

$domainstringrequired
The domain of the new site.
$pathstringrequired
The path of the new site.
$site_idintrequired
Unless you’re running a multi-network install, be sure to set this value to 1.

Return

int|false The ID of the new row

Source

function insert_blog($domain, $path, $site_id) {
	_deprecated_function( __FUNCTION__, '5.1.0', 'wp_insert_site()' );

	$data = array(
		'domain'  => $domain,
		'path'    => $path,
		'site_id' => $site_id,
	);

	$site_id = wp_insert_site( $data );
	if ( is_wp_error( $site_id ) ) {
		return false;
	}

	clean_blog_cache( $site_id );

	return $site_id;
}

Changelog

Version Description
5.1.0 Deprecated. Use wp_insert_site()
MU (3.0.0) Introduced.