Get user roles in function
-
I’m trying to write a function that will send an email to users based on their type after they submit a form. It mostly works, but I know I’m not calling the user roles properly because the email that gets sent always goes to the first type in the function–it never changes, and I’m pretty sure that this is happening because user roles are an array. I’m super rusty w/PHP coding. I haven’t done much in several years, and I wasn’t great at it when I did it regularly. I’d appreciate a little help.
Here’s the function w/comments explaining what I’m trying to do:
// Add filter to send custom email after updates volunteer profile form add_filter ( 'edit_profile_success', 'email_after_edit', 10, 3 ); // this function only applies to this form function email_after_edit($request, $form_name, $user_id){ // if are submitting information for the form on this page: https://tnmentalhealth.dev.opcdev3.nehmedia.com/edit-volunteer-profile/; I know this works if ( $form_name == 'support-line-volunteer-profile-update' ){ // Get user ID, user email address, and user's role $user = get_user_by('id', $user_id); $user_id = get_current_user_id(); $user_email = $user->user_email; $user_meta = get_userdata($user_id); // Everything up to here works $user_roles = $user_meta->roles; $user_roles = implode(', ', $user_meta->roles); return $user_roles; // This is what I want to do: If the user role is User type 1 or User type 2, queue up one version of the email. It the user is type 3, send version 2 of the email if ( $user_roles == ('user_type_1' || 'user_type_2')) { $to = $user_email; $subject = "User types 1 or 2"; $message = '<p>Dear User type 1</p>'; // If the user role is User type 3, send a different version of the email } else if ($user_roles == ('user_type_3')){ $to = $user_email; $subject = "User_type_3"; $message = '<p>Dear User type 3,</p>'; } // User email and email headers $headers = "Content-type: text/html"; // compile email and send ?" wp_mail($to, $subject, $message, $headers); } }
The site is on WP 5.4.1, PHP 7.2. I’m using WP Mail SMTP to send the emails.
I really appreciate any help or advice. Thank you in advance!
Lisa
The page I need help with: [log in to see the link]
- The topic ‘Get user roles in function’ is closed to new replies.