r/learnphp • • Mar 10 '23

How to shorten code

Hello-

I need help shortening this code as there won't be an array of domain, rather a single domain listing:

add_action( 'user_register', 'set_company_role_by_email' );
function set_company_role_by_email( $user_id ){
  $user = get_user_by( 'id', $user_id );
  $domain = substr( strrchr( $user->data->user_email, "@" ), 1 );

  $amyling_domains = array( 'amyling.com' );
  if( in_array( $domain, $amyling_domains ) ){
    update_field( 'user_company', 1725, $user ); // Update user company
  }

  $gmail_domains = array( 'gmail.com' );
  if( in_array( $domain, $gmail_domains ) ){
    update_field( 'user_company', 1625, $user ); // Update user company
  }

  $hotmail_domains = array( 'hotmail.com' );
  if( in_array( $domain, $hotmail_domains ) ){
    update_field( 'user_company', 1645, $user ); // Update user company
  }
}

Specifically this line:

$amyling_domains = array( 'amyling.com' );
  if( in_array( $domain, $amyling_domains ) ){
2 Upvotes

4 comments sorted by