0

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?

1 Answer 1

1

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

1
  • 1
    Appreciate that, it was about 1 am local time and I had been at it for 14 hours. Your code got me 95% of the way there, it still wasn't adding the super-admin as the admin for the created sites, but setting a variable $blog to the result of get_id_from_blog_name() and calling add_user_to_blog() directly, against the recommendation in the references I found did the trick after that. Apr 30, 2022 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.