如何使用PHP脚本在Multisite中动态创建新站点?

时间:2013-09-16 14:54:45

标签: wordpress

如何使用PHP脚本在多站点WordPress安装中创建子网站? 我读过关于wpmu_create_blog()但我不明白如何使用它。

我收到此错误:

  

致命错误:调用未定义的函数wpmu_create_blog()

如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

您错过的部分在此问答中:Creating new blog using PHP。总结一个例子,调整多站点的域和类型。仅出于测试目的,不要将此文件保留在您的实时服务器中。

<?php
# Load WordPress barebones
define( 'WP_USE_THEMES', false );
require( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

# Multisite domain
$main_site = 'example.com';

# Type of Multisite
$subdomain_install = false;

# URL param activated
if( isset( $_GET['new-site'] ) )
{
    # Create a new user
    $rand_number = rand( 1, 2000 );
    $username = 'user-' . $rand_number;
    $password = 'fake-password';
    // $password = wp_generate_password( 12, false );
    $email = "email+$rand_number@example.com";
    $user_id = wpmu_create_user( $username, $password, $email );
    // wp_new_user_notification( $user_id, $password );

    # Create site
    if( $subdomain_install )
    {
        $newdomain = "{$_GET['new-site']}.$main_site";
        $path = '/';
    }
    else
    {
        $newdomain = $main_site;
        $path = "/{$_GET['new-site']}/";
    }
    $title = $_GET['new-site'];
    $blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ) );
    echo "New blog with ID = $blog_id";
} 
else
    echo 'Add to the URL: <b><tt>?new-site=NAME_OF_THE_SITE</tt></b>';