API: Create Private Group
-
Kia ora Robin
Thank you for a BRILLIANT awesome plugin. Much respect. I have bought a couple of the extensions, and would like to donate, but the paypal link seems broken?Also…. For our usage, we will be creating a LOT of private groups (we’re a small charity that supports people who are suicidal… hence the need for private groups for support). Is there any way to do this with an API call, please? It would make our process a gazillion times easier. I see you’ve included some calls in the plugin for joining people to groups, which is cool – but I also need to create a private group for each new person, and would very much prefer not to have to do that through the ui.
Yours hopefully and gratefully, Jamie
-
thanks for your kind comments
ooops on the donate button – I’ll update that very shortly.
in the meantime you can donate via
as for api – so you would like a new group created as part of the registration process – yes ? if so, do you just use WordPress registration?
and then what else would yo like to happen automatically?
Kia ora Robin
Maaaaaan, thank you so much for your incredibly incredibly cool and swift response… incredible…:)) Will hit up that other link.
At the moment, I use a hook to create user. In an ideal world, I’d create a private group, create a forum within that group; change the forum to ‘private’ and then join the new user to it. At the moment, I’m doing all those steps manually, and (a) it’s a heap of admin I could really do without(!), and (b) sooner or later, I (or another one of our volunteer team) will stuff up a step, and someone confidentail journaling will be exposed, arghh!
Mate, if that were possible…. it would be beyond cool…
Thank you again, so kind of you…
Jamie
so what hook are you using ? eg what can I hook to that will give me the user id you’ve just created
Seriously, thank you :))
wpwhpro/run/actions/create_user
Jamie
Kia ora Robin
Thanks again for looking into this possibility. It’s taking a bit to keep up with all our signups at the moment… which leads me also just to double-check; there’s not a limit on how many private groups/forums that can be created, is there?!Gratefully, Jamie
you can run any number of groups.
did you mange to get the donate link working? I’ve fixed it in 3.8.4 just released.
I will get round to looking at your reqs
Yay, downloaded the update and donated.
Look forward very much to your help with creating private group / journal / joining users π
With thanks,
Jamiethis is the guts of the code needed
function rew_add_private_group ($user_id) { /*** FIRST ADD A NEW GROUP ***/ //get user and set up username $user = get_userdata($user_id) ; $username = $user->user_login; //count the number of current user groups and increment by 1 $options = get_option('rpg_groups'); $count=count ($options)+1 ; //set the group up and called it the username $options['group'.$count] = $username; //Update the database update_option('rpg_groups', $options); /*** THEN CREATE A NEW FORUM ***/ $forum_data = apply_filters( 'rew_new_forum_pre_insert', array( 'post_author' => $username, 'post_title' => $username, 'post_content' => '', 'post_parent' => '', 'post_status' => bbp_get_public_status_id(), 'post_type' => bbp_get_forum_post_type(), 'comment_status' => 'closed' ) ); // Insert forum $forum_id = wp_insert_post( $forum_data, true ); //and add meta data update_post_meta ($forum_id , '_bbp_forum_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_topic_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_reply_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_time' , 0 ) ; update_post_meta ($forum_id , '_bbp_forum_subforum_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_status' , 'open' ) ; update_post_meta ($forum_id , '_bbp_forum_type' , 'forum' ) ; /*** THEN SET THIS FORUM TO JUST HAVE THIS GROUP ***/ update_post_meta ($forum_id , '_private_group' , 'group'.$count) ; /*** AND FINALLY SET THE USER TO BE IN THIS GROUP ***/ update_user_meta( $user_id, 'private_group', '*group'.$count.'*'); }so question – what do you want the group and the forum called? – at the moment they are the username, but can be anything that can be automatically generated. You will need to balance having a forum name the user sees as friendly against maybe having several users with the same name – eg John Smith? could look confusing to admins or maybe ok?
BIG WARNING :
This process looks at the number of groups that are there and adds 1 to it to create the new group. It is technically possible that if 2 users were created at the same instant that the same group number would be used. In that case 1 forum might get 2 users allocated to it. The chances of this happening are I think very very small, we are looking at 2 users within say .5 second of each other, probably less. If the creation is automatic – ie done from them registering then this is technically possible. If you do this as part of manual user creation, then it should be entirely possible that you make sure only 1 admin is setting up users at any one time.
If you go with this code as is, then please test it more than once to make sure it is doing what you want. As with my Private Groups plugin, I offer this code with no warranty that it works, but the belief that it does !!
-
This reply was modified 5 years, 9 months ago by
Robin W.
oops, just spotted that you wanted the forum private, so change
'post_status' => bbp_get_public_status_id(),to
'post_status' => bbp_get_private_status_id(),in the code above
Kia ora Robin
You are a LEGEND.
Thank you so much for going to all this trouble – means so much to me. Seriously.
So, would you see there being a problem with this simply being automatically called immediately after a new user is created?
All our new users are entered one at a time, via the API, so, for us, the situation would never arise that two users would be created in such close proximity to create an issue.
We call the private group the user’s fullname (DaveDobbyn) and we call the Forum:
Username’s Journal (DaveD’s Journal).Very very gratefully
Jamie
yes if an api is doing one at a time, then this would not be an issue, and yes called after user is created – you need to pass the user_id from the creation, and ensure that the user has first and last names stored in the user_database.
new version that does the names correctly (hopefully)
function rew_add_private_group ($user_id) { /*** FIRST ADD A NEW GROUP ***/ //get user and set up username $user = get_userdata($user_id) ; $username = $user->user_login; $fullname = $user->first_name.$user->last_name; //count the number of current user groups and increment by 1 $options = get_option('rpg_groups'); $count=count ($options)+1 ; //set the group up and called it the username $options['group'.$count] = $fullname; //Update the database update_option('rpg_groups', $options); /*** THEN CREATE A NEW FORUM ***/ $forum_data = apply_filters( 'rew_new_forum_pre_insert', array( 'post_author' => $username, 'post_title' => $username.'\'s Journal', 'post_content' => '', 'post_parent' => '', 'post_status' => bbp_get_public_status_id(), 'post_type' => bbp_get_forum_post_type(), 'comment_status' => 'closed' ) ); // Insert forum $forum_id = wp_insert_post( $forum_data, true ); //and add meta data update_post_meta ($forum_id , '_bbp_forum_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_topic_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_reply_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_time' , 0 ) ; update_post_meta ($forum_id , '_bbp_forum_subforum_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_status' , 'open' ) ; update_post_meta ($forum_id , '_bbp_forum_type' , 'forum' ) ; /*** THEN SET THIS FORUM TO JUST HAVE THIS GROUP ***/ update_post_meta ($forum_id , '_private_group' , 'group'.$count) ; /*** AND FINALLY SET THE USER TO BE IN THIS GROUP ***/ update_user_meta( $user_id, 'private_group', '*group'.$count.'*'); }corrected for private status
function rew_add_private_group ($user_id) { /*** FIRST ADD A NEW GROUP ***/ //get user and set up username $user = get_userdata($user_id) ; $username = $user->user_login; $fullname = $user->first_name.$user->last_name; //count the number of current user groups and increment by 1 $options = get_option('rpg_groups'); $count=count ($options)+1 ; //set the group up and called it the $fullname $options['group'.$count] = $fullname; //Update the database update_option('rpg_groups', $options); /*** THEN CREATE A NEW FORUM ***/ $forum_data = apply_filters( 'rew_new_forum_pre_insert', array( 'post_author' => $username, 'post_title' => $username.'\'s Journal', 'post_content' => '', 'post_parent' => '', 'post_status' => bbp_get_private_status_id(), 'post_type' => bbp_get_forum_post_type(), 'comment_status' => 'closed' ) ); // Insert forum $forum_id = wp_insert_post( $forum_data, true ); //and add meta data update_post_meta ($forum_id , '_bbp_forum_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_topic_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_reply_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_id' , 0 ) ; update_post_meta ($forum_id , '_bbp_last_active_time' , 0 ) ; update_post_meta ($forum_id , '_bbp_forum_subforum_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_total_reply_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count' , 0 ) ; update_post_meta ($forum_id , '_bbp_topic_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_reply_count_hidden' , 0 ) ; update_post_meta ($forum_id , '_bbp_status' , 'open' ) ; update_post_meta ($forum_id , '_bbp_forum_type' , 'forum' ) ; /*** THEN SET THIS FORUM TO JUST HAVE THIS GROUP ***/ update_post_meta ($forum_id , '_private_group' , 'group'.$count) ; /*** AND FINALLY SET THE USER TO BE IN THIS GROUP ***/ update_user_meta( $user_id, 'private_group', '*group'.$count.'*'); }Kia ora Robin
Ohhhhh YAY!
It works, BEAUTIFULLY.
Utterly cool.
You’re a genius, mate!I’m wondering what’s going to happen with the WP User edit function when, in the future, there are potentially hundreds of groups to select from. Do you think it might barf when it reaches a large size?
Also, quick ask. Does Private Groups mind if two Groups have the same name? That’s bound to happen when we have multiple users with the same name, eh.
I was wondering if you might be able to add a line to your code which adds the user_ud to the Forum (I’m not quite sure what you call this part, but when you create a new forum from the backend, there’s the text section where you can add some narrative, just below the name of the Forum…. If it were possible to dump the user_id there, then when the situation happens that we have multiple forums and groups with the same name, we’ll be able to figure which one relates to which user! Possible?
Thank you again… so very very much
Jamiegroups are happy to have the same name – all code uses the group number, the name is just a label.
Forum names will be unique as they are based on the user_id and that is unique.
user edit function will get long, but still work with hundreds.
the area is post_content, so just find and change this last line
$forum_data = apply_filters( 'rew_new_forum_pre_insert', array( 'post_author' => $username, 'post_title' => $username.'\'s Journal', 'post_content' => '',change last line to
'post_content' => $user_id,or maybe
'post_content' => 'User_id = '.$user_id,Kia ora Robin
You are GOLD.
Thank you – thank you; this is all working beautifully.
I’m so grateful.
I examined, I think, every single suitable plugin combo which would achieve this specific scenario. Yours is the only one.
Much respect, Jamie -
This reply was modified 5 years, 9 months ago by
The topic ‘API: Create Private Group’ is closed to new replies.