The MU forums have moved to WordPress.org

Automatic Registration to Specific Blogs (27 posts)

  1. dhargraves
    Member
    Posted 10 years ago #

    No matter what users do, I'd like all users on my WPMU site to automatically be registered on a specific sub-blog, possibly even a few sub-blogs.

    Removing themselves manually after they login is ok, but ideally they start out with automatic contributor level access.

    I've noticed some plugins that might help people register themselves on different sub-blogs, but this is not what I need.

    To explain the situation a little more, I work for a school system which is using WPMU to host most of our district blogs. Some of these blogs are mandatory and all students need to be able to easily participate. Right now we are having to manually register students into these blogs, which is obviously not very efficient.

    Ideas?

  2. dsader
    Member
    Posted 10 years ago #

    Drop this into a .php file and stick into mu-plugins:`
    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    add_user_to_blog('70', $user_id, 'contributor');
    // adds every new user to blog_id 70
    } ?>
    `

  3. dsader
    Member
    Posted 10 years ago #

    Perhaps a great "school-ish" mu-plugin would be ...
    to create groups at SiteAdmin-->Groups,
    to modify Roles/Caps per user/group at SiteAdmin-->Roles
    to assign users to groups at SiteAdmin-->Users
    and in one click to assign an entire group a blog at SiteAdmin-->Blogs-->Edit.

    The discussion seemed to be going in that direction in this thread too: http://mu.wordpress.org/forums/topic.php?id=9506

  4. dhargraves
    Member
    Posted 10 years ago #

    This is perfect. Exactly what I needed. dsader you rule.

  5. dhargraves
    Member
    Posted 10 years ago #

    Is there any way to make this work for users who have already been registered?

    As far as I could tell this works for new users, but isn't retroactive.

  6. dsader
    Member
    Posted 10 years ago #

    First install WPMU PowerTools
    http://plugins.paidtoblog.com/wpmu-power-tools/
    Then Paste this and execute

    // adds multiple users to multiple blogs ASAP
    $blogs = array(63,70);
    $users = array(51,52,53);
    foreach ($blogs as $blog) {
    foreach($users as $user) {
    add_user_to_blog($blog, $user, 'contributor');
    }
    }

    In this example, I needed users 50-53 to be added to blogs 63 and 70 as contributors.

    Keep a .txt file in your WPMU dir with "one time" snips of code such as this to use again and again.

  7. dsader
    Member
    Posted 10 years ago #

    Screwed up and need to remove mass users from multiple blogs?

    $blogs = array(63,70);
    $users = array(39,40,41,42,51,52,53);
    foreach ($blogs as $blog) {
    foreach($users as $user) {
    remove_user_from_blog($user, $blog); // notice order
    }
    }

  8. dhargraves
    Member
    Posted 10 years ago #

    Thanks dsader, I'll give this a try.

  9. dhargraves
    Member
    Posted 10 years ago #

    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    add_user_to_blog('70', $user_id, 'contributor');
    // adds every new user to blog_id 70
    } ?>

    dsader, is there a way to get this code to work with multiple blogs? I tried duplicating the php file and changing the blog_id, but that ended up giving a fatal error. I'd like to automatically register new users for multiple blogs.

  10. dsader
    Member
    Posted 10 years ago #

    Try the following:

    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    $blogs = array(63,70);
    foreach ($blogs as $blog) {
    add_user_to_blog($blog, $user_id, 'contributor');
    }
    // adds every new user to blog_id 63 and 70
    } ?>

  11. dhargraves
    Member
    Posted 10 years ago #

    Works great, thanks again!!!

  12. scotm
    Member
    Posted 10 years ago #

    Could this also be used to have new members added to a sub-blog only but not members to the root blog?

  13. b.rich
    Member
    Posted 10 years ago #

    @dsader

    Thanks for the tip on WP Power Tools, saved a lot of effort.

    You offered a plug-in that sits in the MU plug-ins folder and will always add a new user to a one or more specified blogs. Because it is in the MU-Plugins folder, does it always run when you add a new user into any of the blogs.

    What I'd like to do is have it so that IF (and only if) I create a used in blog 23 (say) it will always add that user to blog 66( say). If I put the plug-in in the Plug-ins folder for blog 23, will it only work when I add a user in that blog? Or do I need some different code?

    TIA

    Brian

  14. dsader
    Member
    Posted 10 years ago #

    b.rich:
    There is no such thing as a plugins folder "for blog 23".

    The dead end: When the user follows the email link back to start the add_to_blog process, they link to wp-activate.php of the home blog(in my tests), not the sub blog they are added to, therefore regular plugins anywhere else do nothing to the activation process.

    Having said that, I only wonder if the 'add_user_to_blog' hook within the 'add_user_to_blog' function could be used to detect the $blog_id and if it is 23 send out a second automagic email inviting user to join blog 66 ... ... Good luck.

  15. b.rich
    Member
    Posted 10 years ago #

    @dsader

    "There is no such thing as a plugins folder for blog 23" Sorry, a Homer Simpson moment... I meant to say put the plug-in in the Plug-ins folder and activate it for blog 23.

    Anyhow, I see what you are saying - thanks for that pointer. I'll have a look at detecting the blog ID before I run the code. If it works, I'll post it back here.

    One other question - is there a way to suppress the automagic e-mail being sent out?

    (Since the user is already a member of the blog_23 community, in my scenario he doesn't need the second e-mail. In fact, if he gets no e-mail at all from either registration I can work round that.)

    Thanks

    b.rich

  16. dsader
    Member
    Posted 10 years ago #

    The add_user_to_blog function does not generate the email. Existing users are added to blogs silently. But if you use the add_user_to_blog hook to call the add_user_to_blog function ... should php error.

    I haven't tested the wpmu_new_user hook to see if it can pick up the if($blog id==...) condition, though.

    To avoid email:
    1. add existing users via the SiteAdmin-->Blogs-->Edit.
    2. Older versions of WPMU had ways of adding new users without sending email, didn't they? New changes fight spammers/sploggers, I figure.

  17. skozyk
    Member
    Posted 10 years ago #

    dsader, thank you for providing the above helpful plugin code"

    "Drop this into a .php file and stick into mu-plugins:`
    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    add_user_to_blog('70', $user_id, 'contributor');
    // adds every new user to blog_id 70
    } ?>
    "
    How would we modify this to set the blog id to the current blog the user is attempting to register on, instead of a hard coded blog id? In other words, allowing visitors of a WordPress MU site’s child blog to register for the child blog only instead of the main MU blog.

  18. kickoff3pm
    Member
    Posted 10 years ago #

    This is really great, I have setup a general blog to allow anyone to post thanks. But I have a question about these contributers. When these users login it's not easy for them to see how they can post. The page they are sent to doesn't allow them to do much so I can see some of them just registering and giving up.

    Those that do look around the desktop and find the BLOGS option then are able to select not only the GENERAL BLOG but also the DEFAULT blog.

    Is the a way of sending all users who are only contributers to the NEW POST page of the GENERAL BLOG dashboard ?

    I like the idea that someone will register to post and they can do that will as few clicks of the mouse button as possible and don't need to understand the dashboard navigation process.

  19. tracysurf
    Member
    Posted 10 years ago #

    dsader, Solution worked remarkably well. Thanks for contributing!

  20. bitjumper
    Member
    Posted 10 years ago #

    This is great. Worked perfectly. Here is a modification that I did to add all new users as subscribers to all my blogs. I'm rather new to Wordpress plugins, PHP, and SQL, so any feedback, suggestions, or corrections would be welcome!

    <?php
    function add_new_user_to_all_blogs($user_id)
    {
      global $wpdb;
    
      $blog_list = $wpdb->get_results("SELECT blog_id FROM " . $wpdb->blogs);
      foreach ($blog_list as $blog)
      {
        add_user_to_blog($blog->blog_id, $user_id, 'subscriber');
      }
    }
    
    add_action('wpmu_new_user', 'add_new_user_to_all_blogs', 10, 2);
    ?>
  21. caiado.ricardo
    Member
    Posted 9 years ago #

    Hi, DS

    In your example you added 3 users. But how can I add 500 user without declaring each ID in the array $users?

    "$users = array(1,2,3,4,5,...);"?

    // adds multiple users to multiple blogs ASAP
    $blogs = array(63,70);
    $users = array(51,52,53);
    foreach ($blogs as $blog) {
    foreach($users as $user) {
    add_user_to_blog($blog, $user, 'contributor');
    }
    }

  22. dsader
    Member
    Posted 9 years ago #

    I can't test this, but I think this'll do all active users:

    global $wpdb;
    $users = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_status = 0");
  23. GrizzlyGroundswell
    Member
    Posted 9 years ago #

    dsader,

    I have a similar need, maybe 300 though. So would the code I put in Power tools look like this below?

    // adds multiple users to multiple blogs ASAP
    $blogs = array(63,70);
    global $wpdb;
    $users = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_status = 0");
    foreach ($blogs as $blog) {
    foreach($users as $user) {
    add_user_to_blog($blog, $user, 'contributor');
    }
    }

    This has helped me already save a ton of time! But if this works I would be dancing my grizzly jig!

  24. GrizzlyGroundswell
    Member
    Posted 9 years ago #

    Any help here?

  25. dsader
    Member
    Posted 9 years ago #

    As mentioned above, I can't test that code. It should work. Try it on your test server with a handful test users ids. You may need to increase php memory and/or execution time to deal with a larger number of users. Power Tools is cool, but will time out if your server can't deal with the load. Good luck.

  26. GrizzlyGroundswell
    Member
    Posted 9 years ago #

    Hey thanks! I am probably going to leave this one to the experts. I did however, use the above code to move all my "dashboard" new users to my main blog. Now when you sign up for my site you are a contributer. This is huge!

  27. micheletes
    Member
    Posted 9 years ago #

    dsader, you are my master!!

    i want exactly this.

    yeah thank you!

About this Topic

  • Started 10 years ago by dhargraves
  • Latest reply from micheletes