函数文档

create_empty_blog()

💡 云策文档标注

概述

create_empty_blog() 是一个已弃用的 WordPress 函数,用于创建一个空博客。它处理域名、路径和标题等参数,并返回新博客的 ID 或错误信息。

关键要点

  • 函数已弃用:自 WordPress 4.4.0 版本起,此函数被标记为弃用,建议使用替代方法。
  • 参数要求:必须提供域名、路径和博客标题,站点 ID 可选(默认为 1)。
  • 返回值:成功时返回新博客的 ID,失败时返回错误字符串。
  • 核心流程:检查域名是否已存在,插入博客信息到数据库,切换博客并安装,最后恢复当前博客。
  • 相关函数:涉及 domain_exists()、insert_blog()、install_blog()、switch_to_blog() 和 restore_current_blog() 等。

代码示例

function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
    _deprecated_function( __FUNCTION__, '4.4.0' );

    if ( empty($path) )
        $path = '/';

    if ( domain_exists($domain, $path, $site_id) )
        return __( 'Error: Site URL you’ve entered is already taken.' );

    if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
        return __( 'Error: There was a problem creating site entry.' );

    switch_to_blog($blog_id);
    install_blog($blog_id);
    restore_current_blog();

    return $blog_id;
}

注意事项

  • 此函数已弃用,不应在新代码中使用,以避免兼容性问题。
  • 在多站点环境中,需确保域名和路径的唯一性,否则会返回错误。
  • 函数内部依赖多个已弃用或特定函数,如 insert_blog() 和 install_blog(),使用时需注意其状态。

📄 原文内容

Create an empty blog.

Parameters

$domainstringrequired
The new blog’s domain.
$pathstringrequired
The new blog’s path.
$weblog_titlestringrequired
The new blog’s title.
$site_idintoptional
Defaults to 1.

Default:1

Return

string|int The ID of the newly created blog

Source

function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
	_deprecated_function( __FUNCTION__, '4.4.0' );

	if ( empty($path) )
		$path = '/';

	// Check if the domain has been used already. We should return an error message.
	if ( domain_exists($domain, $path, $site_id) )
		return __( '<strong>Error:</strong> Site URL you’ve entered is already taken.' );

	/*
	 * Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
	 * Need to get blog_id from wp_blogs, and create new table names.
	 * Must restore table names at the end of function.
	 */

	if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
		return __( '<strong>Error:</strong> There was a problem creating site entry.' );

	switch_to_blog($blog_id);
	install_blog($blog_id);
	restore_current_blog();

	return $blog_id;
}

Changelog

Version Description
4.4.0 Deprecated.
MU (3.0.0) Introduced.