Skip to:

Bulk add a member to all groups and promote to admin

  • gdams82


    This is actually in reply to The topics closed though and the guy didn’t seem to get it answered. I had the same problem as him and came up with what is probably a terrible solution, but I thought I’d post it anyways in case someone else had the same problem.

    I needed to add and promote a member to admin in 350 groups. So I wrote this script to do it for me. I created a “runonce” wp page and a template for it. Popped the below code into the template file and visited the page to execute.

    Before anyone hassles me, I know this is a terrible way of doing things, but I’m not super familiar with buddypress or using hooks, actions, etc. Like I said I know this is not how things are done, it’s just something I came up with on the fly and it’s how I’m familiar with writing database scripts.

    function add_and_promote() {
    		//Database connection vars
    		$servername = "localhost";
    		$username = "username ";
    		$password = "password ";
    		$dbname = "dbname";
    		// Create connection
    		$conn = new mysqli($servername, $username, $password, $dbname);
    		// Check connection
    		if ($conn->connect_error) {
    			die("Connection failed: " . $conn->connect_error);
    		// Get group ids 
    		$sql = "SELECT id FROM wp_bp_groups";
    		$result = mysqli_query($conn, $sql);
    		//Set member ID here
    		$user_id = 1;
    		//Add member to groups and promote to group admin
    		while($row = mysqli_fetch_assoc($result)) {
    			//Perform operation and set some vars to output 
    			$memberJoin = groups_join_group( $row["id"], $user_id );
    			$memberStatus = groups_promote_member( $user_id, $row["id"], 'admin'); 
    			//Output the vars on the runonce page. Not necessary, but gives you something to look at. 
    			if($memberJoin == true){
     				echo "user $user_id  is in group " . $row["id"];
    			elseif($memberJoin == false){
     				echo "user $user_id  is not in group " . $row["id"];
    				echo 'Error: $memberJoin is not boolean';
    			echo "<br>";
    			if($memberStatus == true){
     				echo "user $user_id is now an admin in group ". $row["id"];
    			elseif($memberStatus == false){
    				 echo "user $user_id is not an admin in group ". $row["id"];
    				echo 'Error: $memberStatus is not boolean';
    			echo "<br><br>Operation Complete";

    If you guys can tell us all the correct way of doing things that would be awesome. I’m sure some one else will need to do this some where down the line and my fix is far from elegant.

    Oh yeah, this takes forever to run on my server, so if it’s a white screen for minute just be patient.

Viewing 1 replies (of 1 total)

  • gdams82


    BTW, that was tested in WordPress 4.4.2 and BuddPress 2.5.2 on local XAMPP running Apache/2.4.17, PHP/5.6.15, custom theme, 25 plugins.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar