• Add-ons
  • Blog
  • Resources
    • Documentation
    • FAQs
    • Code snippets
    • Use cases
    • Support
  • Account
    • My account
    • Checkout
    • Cart
  • English
  • French
  • Add-ons
  • Blog
  • Resources
    • Documentation
    • FAQs
    • Code snippets
    • Use cases
    • Support
  • Account
    • My account
    • Checkout
    • Cart
  • English
  • French
home/Knowledge Base/Code snippets/Automatically add a user to a group

Automatically add a user to a group

244 views 0 April 13, 2021

Customer Area adds the concept of user groups with two of its add-ons: simple user groups in the “Additional owner types” add-on and managed user groups in the “Managed Groups” add-on. Sometimes, you would like to automatically add a user to a group when he registers.

You can include these simple snippets inside your theme’s functions.php file or inside your Customer Area theme’s cuar-functions.php file.

For “Additional owner types” user groups

function cuar_add_new_user_to_group( $new_user_id ) {
  $destination_group_id = 123; // put the real group ID here

  $group_addon = cuar_addon('user-group');
  $group_addon->add_user_to_group( $new_user_id, $destination_group_id );
}
add_action( "user_register", "cuar_add_new_user_to_group" );

For the “Managed Groups” add-ons.

Please note that in this case, you can either add the user as a manager of the group or as a simple member (or both if you feel this is appropriate).

function cuar_add_new_user_to_managed_group( $new_user_id ) {
  $destination_group_id = 123; // put the real group ID here
  $is_manager = false; // set to true if you want that user to be a manager within that group
  $is_member = true; // set to true if you want that user to be a simple member of that group

  $group_addon = cuar_addon('managed-groups');

  if ( $is_manager ) {
    $group_addon->add_manager_to_group( $new_user_id, $destination_group_id );
  }
  if ( $is_member ) {
    $group_addon->add_member_to_group( $new_user_id, $destination_group_id );
  }
}
add_action( "user_register", "cuar_add_new_user_to_managed_group" );
Tags:ownershipgroups

Was this helpful?

Yes  No
Related Articles
  • Custom admin notification’s recipients
  • Disable CSS resets and allow integration of third party plugins
  • Changing the default page shown by the Customer Area
  • Changing the page shown after logout
  • Sidebars: how to disable them
  • Detect if you are on a page in the customer area
Knowledge base
  • Code snippets 25
  • Use cases 2
  • Community links 5
FAQ
  • About licences 7
  • Pre-sales questions 3
  • After-sales questions 1
  • Plugin usage 8
  • Technical questions 10
  • Payments 4
Documentations
  • Getting started
  • Using code snippets
  • The template system
  • Full documentation
Support
  • Pre-sales Enquiries
  • Main plugin support
  • Premium add-ons support
  • Feature Requests
  • Terms and conditions for sales
  • Refund policy
  • Privacy Policy
  • Legal notices
  • Copyright 2021 wp-customerarea.com. All Rights Reserved.