0

I let users create sites not via the standard registration method rather via a custom plugin. I use wpmu_create_blog, then add the user to the new blog as a user with a custom role and then and then wp_redirect the user to the new site's admin:

$new_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
add_user_to_blog($new_blog_id, get_current_user_id(), 'my_custom_role');
$blog = get_blog_details($new_blog_id,true);
$blog_url = $blog->siteurl;
$redirect_url = $blog_url . '/wp-admin';

wp_redirect( add_query_arg(array(
    'custom_notice' => 'a_custom_notice'
),$redirect_url));

What happens is, the user lands on the new site admin with a message: Sorry, you are not allowed to access this page:

enter image description here

Then when they hit refresh all is suddenly good...

How do I make the new site's admin accesible to the new user on the 1st attempt?

3
+50

TLDR

user_roles option is broken, it can be fixed by updating the db, see the solution below.

Who & Why

I was able to recreate your issue and analyze it. As you can see in the error it is clearly a matter of permissions and capabilities, and it turns out that the whole thing is because wp_xx_user_roles option.

When avoiding the regular registration process, as you are trying to do, the globals $wpdb and $wp_roles are already exist, and when you use wpmu_create_blog from within your site, the very-important option wp_xx_user_roles (the xx is for the blog id) is written with the current viewed blog id, and not the new one just created.

My guess is that when creating a new blog from the registration page, wordpress is not considering any blog to be the current blog, so when it uses a brand new wp_roles class instance, instead of using the already-set globals $wpdb and $wp_roles;

I couldn't find any civil way to address this issue, so we'll do it ugly. The ugly solution is to update the blog id in user_roles option name after creating a new blog, like so:

    function replace_wrong_user_roles_option( $new_blog_id ){
        global $wpdb, $wp_roles;

        $current_blog_id = get_current_blog_id();
        if( $current_blog_id == $new_blog_id )
            return true;

        $prefix_length        = strlen( $wpdb->prefix );
        $blog_prefix_length   = strlen( $new_blog_id.'_' );
        $blog_prefix_position = strrpos ( $current_prefix, $new_blog_id.'_' );

        // only on new wpmu, when main blog prefix is without the blog_id
        if($current_blog_id == 1 && !$blog_prefix_position){
            $new_blog_prefix = $wpdb->prefix.$new_blog_id.'_';
        }else{
            $new_blog_prefix = str_replace(get_current_blog_id(), $new_blog_id, $wpdb->prefix);
        }

        return $wpdb->update(
            $new_blog_prefix.'options',
            array( 'option_name' => $new_blog_prefix.'user_roles' ),
            array( 'option_name' => $wpdb->prefix.'user_roles' )
        );
    }

And you can insert that function call right after you create your blog:

    $new_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
    replace_wrong_user_roles_option( $new_blog_id );
    add_user_to_blog($new_blog_id, get_current_user_id(), 'my_custom_role');
    $blog = get_blog_details($new_blog_id,true);
    $blog_url = $blog->siteurl;
    $redirect_url = $blog_url . '/wp-admin';

    wp_redirect( add_query_arg(array(
        'custom_notice' => 'a_custom_notice'
    ),$redirect_url));
  • all in all you are right. I didn't use your solution but it did point me to find the true bug: I created the role only after adding the user to the blog, so I switched the order and things work now. That said, my problem is solved and I mark your answer as correct. – shaharsol Oct 17 '17 at 16:29
  • @shaharsol I guess it is fair to mark this answer as correct to award the bounty. But for other users having a similar problem, you should post the correct solution for this problem as a new accepted answer afterwards. Having it in a comment can be overseen to easily. – JHoffmann Oct 20 '17 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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