Conditionally add new user to Default Group

I have two forms on the front end of the site that I am using to add users. Each form adds a different user level in WordPress but as of now both get added to the same default newsletter group set in the General Settings.

Is there a way to bypass the Default Group on the settings page and add each conditionally?

  • Market Mechanics

    This is how I've been trying to do it but have had no luck. There must be an easier way.

    //* If a guest user is added then add the user to the newsletter as a guest
    add_action( 'gform_after_submission_4', 'change_newsletter_group_to_guest', 10, 2 );
    
    function change_newsletter_group_to_guest($entry, $form) {
    
    	global $wpdb;
    
    	//* Get the username from the submitted form
    	$username = $entry['5'];
    
    	//* Grab all the guest info from the database
    	$new_guest_id = $wpdb->get_results( 'SELECT ID FROM epa_users WHERE user_login = $username');
    	$new_guest_member_id = $wpdb->get_results( 'SELECT member_id FROM epa_enewsletter_members WHERE wp_user_id = $new_guest_id');
    	$guest_id = array_map('current', $new_guest_member_id);
    
    	//* Replace the user group with ehe guest group id
    	$wpdb->update( 'epa_enewsletter_member_group', array( 'group_id' => 2, 'member_id' => $guest_id ), array( '%s', '%d' ) );
    
    }
  • Market Mechanics

    I was finally able to get it to work. Not sure if it's the best way to go about it but it does the trick.

    function submitted_guest_data() {
    
    	//* Connect to the WordPress database
    	global $wpdb;
    
    	//* Get the username from the submitted form
    	$username = $_GET['Username'];
    
    	//* Grab all the guest info from the database
    	$new_guest_id = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM epa_users WHERE user_login = %s", $username ) );
    	$new_guest_id_clean = array_map('current', $new_guest_id);
    	$clean_guest_id = $new_guest_id_clean[0];
    
    	$new_guest_member_id = $wpdb->get_results( $wpdb->prepare( "SELECT member_id FROM epa_enewsletter_members WHERE wp_user_id = %s", $clean_guest_id ) );
    	$new_guest_member_id_clean = array_map('current', $new_guest_member_id);
    	$clean_guest_member_id = $new_guest_member_id_clean[0];
    
    	//* Replace the user group with ehe guest group id
    
    	$wpdb->update( 'epa_enewsletter_member_group', array( 'group_id' => 2),array('member_id'=>$clean_guest_member_id));
    
    }
    
    add_shortcode('submitted-guest-data', 'submitted_guest_data');
  • Adam Czajczyk

    Hello @Market Mechanics,

    I hope you're well today and thank you for your question!

    It seems I reached this thread after you've already came up with a solution. This looks good and it's surely good if it works. That said, I'm sorry for not getting to you earlier and thank you very much for sharing this solution with our community.

    I'm sure some other members will benefit from it, so I made sure that some points fly your way :slight_smile:

    Cheers,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.