lafif-a
10/27/2014 - 7:46 AM

[Wordpress] [Buddypress] Batch add user to buddypress group

[Wordpress] [Buddypress] Batch add user to buddypress group

<?php
/**
 * Batch Add user to buddypress group
 */

add_action('load-users.php',function() {
    if(isset($_POST['action']) && ($_POST['action'] == 'groupadd') && isset($_POST['bp_gid']) && isset($_POST['allusers'])) {
    // print_r($_POST); exit();
    
    $group_id = $_POST['bp_gid'];
    $users = $_POST['allusers'];
    foreach ($users as $user_id) {
        groups_accept_invite( $user_id, $group_id );
    }
}
    //Add some Javascript to handle the form submission
    add_action('admin_footer',function(){ ?>
    <script>
        jQuery("select[name='action']").append(jQuery('<option value="groupadd">Add to Group</option>'));
        jQuery("#doaction").click(function(e){
            if(jQuery("select[name='action'] :selected").val()=="groupadd") { 
            e.preventDefault();
                gid=prompt("Please enter a BuddyPres Group ID","1");
                jQuery(".wrap form").append('<input type="hidden" name="bp_gid" value="'+gid+'" />').submit();
            }
        });
    </script>
    <?php
    });
});


?>