Forums

Disable main site access on new user creation for subsite? (9 posts)

  1. bwinn
    Member
    Posted 1 year ago #

    Hi,

    I am having a problem with creating new users within my multisite setup. I want each user to have only access to their site. So I have a login on the homepage and everytime a user logs in it takes them to homesite/wp-admin as opposed to theirsite/wp-admin.

    Any help?

  2. David Sader
    Member
    Posted 1 year ago #

    I have a login on the homepage

    That is why they go to homesite/wp-admin/.

    To redirect a successful login you will need to drop something like this into your "mu-plugins":

    <?php
    function ds_login_redirect($redirect_to, $request_redirect_to, $user)
    {
        if ($user->ID != 0) {
            $user_info = get_userdata($user->ID);
            if ($user_info->primary_blog) {
                $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
                if ($primary_url) {
                    wp_redirect($primary_url);
                    die();
                }
            }
        }
        return $redirect_to;
    }
    add_filter('login_redirect','ds_login_redirect', 100, 3);
    ?>

    Users without a blog must be subscribers of at least the main blog. Where will users without a blog go when they login? They see only a subscribers limited Dashboard and profile anyway. The SuperAdmin->Options allows you to designate an alternate blog as the "Dashboard Blog", wordpress.com calls theirs the "Global Dashboard".

  3. bwinn
    Member
    Posted 1 year ago #

    I dont think this is necessary. I do not have an open registration I should add, so I am adding people on a one by one basis. All users will have their own sites. So there wont be users that don't have a blog.

    Forget that there is a login on the homepage... Is there a way to just give users access to their own site and not have them be associated with the main site in any way shape or form?

    I had this working before, but I updated my plugins and now it doesnt work anymore. The users were getting directed to their site, once they logged in from the homepage login modal.

    Thanks for your quick response.

  4. David Sader
    Member
    Posted 1 year ago #

    Something like the following in "mu-plugins"?

    add_action( 'wpmu_new_blog', 'ds_new_user_meta', 10, 2 );
    function ds_new_user_meta($blog_id, $user_id) {
    remove_user_from_blog( $user_id, '1' ); //removes new user from blog_id 1
    }

    Remove existing users from blog 1 the usual way.

  5. bwinn
    Member
    Posted 1 year ago #

    Im not the greatest with php. If I were to add this code to MU plugins, would it work just like you have it there or would I need to add some more php to it?

    Thanks

  6. bwinn
    Member
    Posted 1 year ago #

    Ok so I sort of found out what the problem is. Only users that are the admins are getting redirected. If they are set up as an author or something other than admin, they are just going to homesite/wp-admin.

    Wondering if there is anyway to make this work....

  7. David Sader
    Member
    Posted 1 year ago #

    Both snippets above work for me, just make sure any php is between php tags: <?php ... ?> and is in a *.php txt file with at least 644 permissions.

    The login redirect plugin above should work for all users. That said, I did write a plugin to allow the SuperAdmin to edit a users primary site via their profile. You may find it useful when a user is a member of more than one blog, but redirects to the wrong site as their primary. http://wordpress.org/extend/plugins/primary-blog-switcher-for-superadmins/

    If you are adding users one by one via the back end, you may have to just remove them from blog 1 one by one as well. When the user is created, they will be added to the dashboard blog, the snippet above will only remove them from blog 1 when they create a new blog.

  8. bwinn
    Member
    Posted 1 year ago #

    Thanks so much.

    Will it remove the user from the main blog upon creation of their account, or when I drop this code into the MU plugin directory?

  9. David Sader
    Member
    Posted 1 year ago #

    "when they create a new blog."

Topic Closed

This topic has been closed to new replies.

About this Topic