Forum Replies Created
-
I don’t know why, but I have found a solution. As ‘check_slug’ worked, I just took the code from that function, amended it to return the group id instead of the slug, and included it in my function – which worked. Be good to understand why none of the other functions worked though?
function ofc_add_registrant_to_group($registration, $old_STS_ID, $new_STS_ID, $context) { global $wpdb; $user_id = get_current_user_id(); if (! $registration instanceof EE_Registration ) { // No EE_Registration, get out return; } $event_id = $registration->event_ID(); $subgroups = get_field('event_sub_group', $event_id); $slug = $subgroups[0]->post_name; // These two lines made the difference $bp = buddypress(); $group_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE slug = %s", $slug ) ); if ( ! groups_is_user_member($user_id, $group_id )) { groups_join_group($group_id, $user_id); } } add_action('AHEE__EE_Registration__set_status__to_approved', 'ofc_add_registrant_to_group', 10, 4);
I have set up an Advanced Custom Field that is linked to the event, and the admin sets the group that the user should be added to on registration for the event. The field is called event_sub_group and it returns a WP_Post object. From that, I can get the slug of the group in the post_name. I was then trying to get the group ID using the slug, so I can add the user to the group. This is my current code.
/Add user to buddyboss group when they register for an event function ofc_add_registrant_to_group($registration, $old_STS_ID, $new_STS_ID, $context) { global $bp, $wpdb; $user_id = get_current_user_id(); if (! $registration instanceof EE_Registration ) { // No EE_Registration, get out return; } $event_id = $registration->event_ID(); $subgroups = get_field('event_sub_group', $event_id); $slug = $subgroups[0]->post_name; $group_id = BP_Groups_Group::group_exists( $slug ); if ( ! groups_is_user_member($user_id, $group_id )) { $bp->groups->current_group = groups_get_group(array('group_id' => $group_id)); groups_join_group($group_id, $user_id); } } add_action('AHEE__EE_Registration__set_status__to_approved', 'ofc_add_registrant_to_group', 10, 4);
BP_Groups_Group::group_exists( $slug ) returns nothing. If I use BP_Groups_Group::check_slug( $slug ) – it returns the correct slug name as expected.
As a follow up:
BP_Groups_Group::check_slug($slug) returns the slug correctly.I am not sure how, but I managed to get the group id on one occasion, but it’s back to not working again. I was using BP_Groups_Group::get_id_from_slug($slug) when I got the group id.