I cannot add user to the discouse forum from a wordpress website when user added in a membership

You can’t add users to the “moderators”, “admin”, or “staff” groups with this function call. Technically, you can’t add users to any “automatic” groups with this approach. You can only add users to custom groups that you have created.

Edit: if you need to add users to the moderators group, have a look at the sync_sso_record function: https://github.com/discourse/wp-discourse/blob/main/lib/utilities.php#L70-L81. The add_user_to_discourse_group function is just a convenience wrapper around sync_sso_record.

moderator (boolean) is an accepted argument for the SSO params: discourse/lib/discourse_connect_base.rb at 5098338a9649ee8830c8a0781e0eb538e8206eac · discourse/discourse · GitHub. You could also add any custom groups that you want to add users to by using the add_groups param. That would allow you to add a user to the “moderators” group and the “Volunteer Members” group with a single API call.

Edit 2: If you try the sync_sso_record function approach, you might need to use the string "true" instead of the boolean true for the moderator param. PHP interprets true as 1. I think Discourse accepts the string "true" to deal with that case. Note that you can use the get_sso_params function to set the SSO params: https://github.com/discourse/wp-discourse/blob/main/lib/utilities.php#L129. There’s an example of how to do that in the add_user_to_discourse_group function’s code.