Programmatically creating site in a network

Goal

Create multiple, predefined sites using wordpress multisite on plugin activation.

Current Code

    $domains = array('however', 'many', 'hardcoded', 'subdomains',);

    foreach($domains as $domain){
        $meta = array(
            'public' = 1,
            'WPLANG' = '',
        );
        $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', get_network()-domain );
        $user_id  = get_option('admin_email');
        $id = wpmu_create_blog( $newdomain, get_network()-path, $domain, $user_id, $meta, get_current_network_id() );
        if ( ! is_wp_error( $id ) ) {
            update_user_option( $user_id, $domain, $id, true );
        }
    }
}

Current Functionality

The code creates the sites and they are listed on the sites page in the network admin panel.

Current Issues

Sites created with this code don't have a user set as admin. The sites are also not listed in the 'my sites' drop down., but I'm willing to bet that it's because there's no admin set on the sites.

Where I need help

I've sifted through the files in the wp-admin/network and can't figure out what function I'm needing to incorporate that sets a user as an admin of a subnetwork site. What function would I need to finish off the plugin activation code?

Topic network-admin subdomains multisite Wordpress

Category Web


You're using $user_id = get_option( 'admin_email' ); and then using that $user_id. However, get_option( 'admin_email' ); returns an email address, not the int that wpmu_create_blog() expects. So your sites are (presumably) being created with a user_id of 0.

Here's how I'd address this:

$user_email  = get_option('admin_email');
$user_object = get_user_by( 'email', $user_email );
if ( false !== $user_object ) {
    $user_id = $user_object->ID;
}
$id = wpmu_create_blog( $newdomain, get_network()->path, $domain, $user_id, $meta, get_current_network_id() );

References

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.