Forums

How to automatically add ALL new users to main blog? (23 posts)

  1. agreda
    Member
    Posted 3 months ago #

    The Dashboard Blog option reads ...

    New users are added to this blog as subscribers (or the user role defined below) if they don't have a blog.

    But, here is my issue ...

    I've noticed that new users who do create their own blog upon sign-up are not being added to our main blog.

    We've left our "Dashboard Blog" option blank, so users who do not create a blog are getting added to the main blog.

    But we are running Simple:Press forums on the main blog only so we need all new users to be added as main blog subscribers, so they become forum members by default. Whether they create their own blog upon sign-up or not.

    How can I make this happen automatically – without having to manually add users who have just registered, to the main blog, sending them yet another confirmation email?

    Thank you all in advance for your help!

    PS: Our site is at http://tripawds.com

  2. dsader
    Member
    Posted 3 months ago #

    I'd add the following two mu-plugins.

    First to add new users to blog 1:

    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    			add_user_to_blog('1', $user_id, 'subscriber');
    }

    Second to migrate all existing users to blog 1:
    Install WPMU PowerTools in mu-plugins and "Run this code once" the following snippet:

    global $wpdb;
    	$query = "SELECT ID FROM {$wpdb->users}";
    	$users = $wpdb->get_results( $query, ARRAY_A );
    foreach($users as $user) {
    add_user_to_blog('1', $user['ID'], 'subscriber');
    }
  3. agreda
    Member
    Posted 3 months ago #

    @dsader Thank you for the quick response! I took care of migrating existing users in our recent migration from wp to wpmu, so I'll keep that Power Tools tip in mind for our next major move project.

    But regarding your first tip, exactly what file gets edited there? You mention two mu plugins. And forgive my ignorance, but what do the 10, 2 values represent?

    Thanks again.

  4. dsader
    Member
    Posted 3 months ago #

    The first is a new plugin for mu-plugins. Create a new php file and upload it to mu-plugins.

    The 10 is the load order $priority, 10 is default by the way. When many add_actions are added to the same do_action, the order each fires may be an issue. In this case, order is no issue.

    2 denotes the number of $accepted_args the function takes, which in the above example is wrong, should be 1 - my bad. 1 is the default as well.

    Therefore,
    add_action( 'wpmu_new_user', 'ds_new_user_meta');
    would work just the same.

    http://codex.wordpress.org/Function_Reference/add_action

  5. layotte
    Member
    Posted 3 months ago #

    For some reason I cannot get this to work for me...

    function ds_new_user_meta($user_id) {
    add_user_to_blog('1', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_user', 'ds_new_user_meta' );

    is in a file called new_user.php.

    It works fine if I change the blog id to 3:

    function ds_new_user_meta($user_id) {
    add_user_to_blog('3', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_user', 'ds_new_user_meta' );

    It's just not allowing me to add it to the main site. Any ideas/tips? Using WPMU 2.8.3 + BuddyPress 1.0.1.

    Also, I've put in some fwrites in the add_user_to_blog function in wpmu-functions.php to get a little debug info. Outputting blog_id, user_id, role, domain:

    1
    61
    contributor
    domain.com

    56
    61
    administrator

    So it's obviously getting to the point where it's trying to add the user to blog id 1, as the correct role.

  6. layotte
    Member
    Posted 3 months ago #

    Ok... so I got it working.

    function ds_new_user_meta($blog_id, $user_id) {
    add_user_to_blog('1', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_blog', 'ds_new_user_meta', 10, 2 );

    Seems to be working like this...

  7. agreda
    Member
    Posted 3 months ago #

    Thanks! but please excuse my ignorance...

    I presume I can just change 'contributor' to 'subscriber' to change their role. That's easy enough.

    But more importantly, where does this code go? Is it a core file (new_user.php) edit like layotte suggests? Or, does it go in mu-plugins as per dsader's instructions? And if the latter, what esle needs to be added to the file? (I've never written a plugin obviously!)

    Thanks for your help and patience ... we're learning here!

  8. layotte
    Member
    Posted 3 months ago #

    agreda,

    I created the new_user.php file, added the code to that file, and placed the code in mu-plugins (as dsader recommended).

    Don't forget to start and end the file with <?php and ?> (respectively).

    Hope that helps.

    Lew

  9. layotte
    Member
    Posted 3 months ago #

    Oh, and yes, you can change 'contributor' to 'subscriber'...

  10. agreda
    Member
    Posted 3 months ago #

    Thanks! I really appreciate the education. Will give it a shot.

  11. ToreGu
    Member
    Posted 3 months ago #

    This was very important to me! Please make this a part of WP canonical!

  12. ToreGu
    Member
    Posted 2 months ago #

    Hi again!

    I thought I got this working but I was wrong.

    I'm using the latest WPMU and also Buddypress. Buddypress seems to be quite dissatisfied with this and gives this error message:

    Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxx/public_html/xxxxxx.com/wp-content/mu-plugins/new-user.php:8) in /home/xxxx/public_html/xxxx.com/wp-content/plugins/buddypress/bp-xprofile/bp-xprofile-signup.php on line 5

    And the graphics in the blog looks wrong.

    I've added this code in new_user.php (in wordpress_mu plugins folder):

    < php ? (but so that it works)

    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    			add_user_to_blog('2', $user_id, 'author');
    }

    ?>

  13. dsader
    Member
    Posted 2 months ago #

    Remove the blank lines. The error says there is a problem with line 8, but the snip above has only 4 lines. Extra white space before or after php tags is not so good.

    http://codex.wordpress.org/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F

  14. ToreGu
    Member
    Posted 2 months ago #

    Thanks for the help. I tried a lot with the file but had no success. I restarted with a new file and voilá.

    And also thanks for the code! Works perfect!

  15. jlm99
    Member
    Posted 2 months ago #

    This is terrific, and I've added it, BUT it doesn't seem to work if a sub-blog administrator adds a new user in their own blog's backend. Is there a second action hook needed for that? I'm trying to track it down myself, but any pointers would help!

  16. PBO
    Member
    Posted 1 month ago #

    I finally got this to work thanks for posting this in the thread above. BUT I Have a problem

    using this PHP code

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    			add_user_to_blog('2', $user_id, 'subscriber');
    }
    ?>

    I have a main blog and a forums.domain.com

    users now create a blog and is automatically added in the forum blog which ID is 2.

    Now the problem is when new users sign the forums blogs becomes the primary blog and not the blog they have created. Is there a way to fix this.

    Thanks

  17. PBO
    Member
    Posted 1 month ago #

    Here is the solution

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
    update_usermeta($user_id, 'primary_blog', $blog);
    }
    ?>
  18. jlappas
    Member
    Posted 2 weeks ago #

    I need to allow users to register to blog = 2 but not automatically be added to primary blog = 1. I I have tried to use this code in mu-plugins:

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
    update_usermeta($user_id, 'primary_blog', $blog);
    }
    ?>

    This code allows new users to sign up in blog 2 correctly but it also registering them for the primary blog 1. How can I get them to only register for blog 2 without being added to blog 1. I have tried adding a line to remove the user from blog 1 within this code but have not been successful. I think WP code automatically adds all users to blog 1. Can you please help?

  19. agreda
    Member
    Posted 2 weeks ago #

    I need to allow users to register to blog = 2

    Why not just allow them to register by setting the "users can register" value to 1 for blog id 2? Just visit /wp-admin/wpmu-blogs.php?action=editblog&id=2 and look under Blog options (wp_2_options).

    Someone else might be able to confirm if this will do the trick, but I believe that is all that is needed to allow users to register for a specific blog. No plugin necessary! Good luck.

  20. dsader
    Member
    Posted 2 weeks ago #

    jlappas, yes WPMU adds users to blog 1 automagically, if there is no "dashboard blog" set in SiteAdmin->Options. If blog 2 is your "Dashboard Blog" change the setting there and no plugins are needed.

    Look for

    add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );

    in wp-includes/wpmu-functions.php

    remove_user_from_blog should still work in a plugin during the wpmu_new_user action perhaps at a later priority, change "10" to 99:

    http://codex.wordpress.org/Function_Reference/add_action

    agreda's "users can register" suggestion shouldn't work as there is no blog by blog registration.php in WPMU. wp-signup.php redirects signups from the main blog only.

  21. agreda
    Member
    Posted 2 weeks ago #

    Apologies for the misunderstanding. I realized later by "I need to allow users to register to blog = 2", jlappas actually meant, upon signup.

    It is my understanding that admins can use this setting to allow already registered site members to register for individual blogs.

  22. circuit
    Member
    Posted 2 weeks ago #

    i found this thread after posting my problem; users who create a blog are removed from the main blog.

    http://mu.wordpress.org/forums/topic/15375?replies=2

    i have almost exactly the same issue, but none of the above solutions work.

    thought i would create a link between the two topics in case this is useful for others in the same situation as me.

  23. jlappas
    Member
    Posted 1 week ago #

    Big help. I have it all working now. Thanks guys.

    dsader - options did the trick. I also followed the post in http://mu.wordpress.org/forums/topic/15375?replies=2

    Worked to help keep subscribers on blog 1 when new blog is created (this was confusing at first because I thought it was happening when users were added with no blog created.)

    Thanks again!

Reply

You must log in to post.

About this Topic