Automatically adding users to all blog on your wpmu/wp multisite blog network

Jotted by brajesh on June 27, 2010

There are times when you want all of the registered member on your blog network to be members of all the blog. WordPress does it differently. By default all the registered users are added to the main blog with the role you have specified(or “subscriber” by default ). If the user creates a new blog, they are removed from the main blog and added to the new blog they created.

My solution here will allow to automatically add users to all blogs on your network, it does not matter much if they register a new one or they do not have a blog but just a registered member. In case the user registers new blog, this function will do no harm the user will be admin of the new blog as in normal case.

To our help, there are two functions

is_blog_user():-It checks whether the current logged in user is a member of the current blog or not.

add_user_to_blog($blog_id,$user_id,$role):- it allows you to add an existing user to a blog with the specific role where $role can be “subscriber”,”author”,”editor”,”contributor” or “administrator”

Now the code, putting the following code in your theme’s functions.php will do the job or you may wrap it as a plugin in case you want it that way

 
add_action('wp','bpdev_add_user_to_blog',10);//hook our function
 
/*add logged in user to current blog*/
function bpdev_add_user_to_blog(){
if(!is_user_logged_in())
return false;//do not do anything
 
global $current_user,$blog_id;
 
if(!is_blog_user())//check for current membership
add_user_to_blog($blog_id, $current_user->ID, "subscriber");//give subscriber role for current blog
 
}

And if you are using buddypress, you can put the above code in bp-custom.php.

What it does:-

When a logged in user visits a blog on your network, it checks whether he/she is a member of the current blog or not. If not, it adds them as a member to the current blog.

I hope it helps in some of the cases. Let me know it it works for you or not ?

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]
If you enjoyed this post, make sure you subscribe to my RSS feed!
Grab my other free plugins here at wordpress Plugins repository.
We will keep coming with more informative tips,tricks,tutorials to keep you updated with wordpress/buddypress/wordpress mu regularly.If you want to help us,You can ,by spreading the words.We provide free and premium plugins/support for wordpress Mu,buddypress at a very low cost (starting with $30 for 3 months membership).If you or anyone you know,needs some real good plugins/themes for their wordpress Mu/buddypress powered site, Please refer them to http://BuddyDev.com.We will highly appreciate your support and keep coming with more useful and free stuffs.


10 Responses to “Automatically adding users to all blog on your wpmu/wp multisite blog network”

  1. mercime says:

    This is cool, Brajesh. Will check it out, thank you.

  2. alex says:

    excellent function! Just copy paste it works ! Thanks !

  3. Jacksondoe says:

    Thanks a lot! I didnt expect this to work on my new 3.0.3 WordPress multisite installation, fearing the code would have been outdate by now, but it worked flawlessly!

    Well done.

  4. Venu says:

    I have searched for a plugin with similar functionality( To add users automatically) . now luckily , I found this article. Thanks for the tip

    Does it still works on new WP versions?

    Thank you !

  5. Venu says:

    Brajesh ,

    Usually, this should be nice if there is an option to let the user to proceed if he want to join.

    I mean that … an option button ” Let me Join ” should display some where on the sidebar or footer. If the user want to join to that sub-site he can click on that button and he will add to the site.

    So , in that way it avoids every user who visited that sub-site to join automatically . Indeed, Not every user likes to join . It helps the super admins and the particular subsite admins too

    Can you please help me with the code?

    Or can you help me to update this old plugin with similar functionality ? There is a plugin, ‘add side bar users’ for this. But it is not fully compatible with newer versions- http://wordpress.org/support/topic/incompatibility-with-newer-versions . Some other user posted a query about this on SE site – http://wordpress.stackexchange.com/questions/21728/problem-with-plugin-and-help-needed-to-customize

    Thank you!

  6. Nahum says:

    Been searching for the capability that @Venu mentioned above for a couple of weeks.

    I remember making use of this code a while back and it would be cool to let members have the option. beyond that, a way to let them choose from a set of custom user roles.

  7. brajesh says:

    Hi Venu, Nahum,
    Thank you for the comments. I will put the code as a widget today. Please check back in the morning for the widget.
    @Nahum,
    will try to add the roles functionality in the first release but not sure if I will or not .

    • Nahum says:

      That is very nice. When i saw my search landed me back on this site/page and I saw it was brajesh, I knew I was in a good place for solutions! thanks so much brajesh.

      Assigned a default role is sufficient but I would love to see that added functionality to let admin/user choose. I’ve seen the roles functionality http://octalforty.com/articles/assigning-role-on-wordpress-registration-page/ for registration but that doesn’t help with an existing multisite/bp network with existing members who need to select from a set of custom roles at the time of first login to the site to be able to participate in the site with the right role.


Leave a Reply


Get Adobe Flash playerPlugin by wpburn.com wordpress themes