Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically create a new site in Multisite with a PHP script?

Tags:

wordpress

How can I create a subsite in a Multisite WordPress installation with a PHP script?
I've read about wpmu_create_blog() but I don't understand how use it.

I receive this error:

Fatal error: Call to undefined function wpmu_create_blog()

How to solve this?

like image 796
Francesco Avatar asked Sep 16 '13 14:09

Francesco


Video Answer


1 Answers

The part you miss is in this Q&A: Creating new blog using PHP. Summed up in an example, adjust the domain and type of Multisite. For testing purposes only, don't leave this file in your live server.

<?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 protected]";
    $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>';
like image 104
brasofilo Avatar answered Sep 26 '22 17:09

brasofilo