Support » Developing with WordPress » Get user roles in function

  • Resolved cowanservices

    (@cowanservices)


    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: http://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]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your function has a parameter of $user_id, which you use to look up the user. Then you reset it to the current user. Then you have a mixture of data from the user you looked up and the current user. Your flow might make these the same, but the code should be one or the other, not both. Since it should be just one user, not two, there is no need to get the data twice (once with get_user_by and once with get_userdata).

    Instead of using implode to change the roles from an array to a string, you should leave it as an array and use in_array() to check for the role you want.
    if ( in_array( 'user_type_1', $user_roles ) )

    Thread Starter cowanservices

    (@cowanservices)

    Thanks for the quick reply, Joy! I’ll give this a try!

    Lisa

    Thread Starter cowanservices

    (@cowanservices)

    Thank You, Joy! After some tweaks and testing, it works! I’ve been working on this for 3 weeks. I cannot tell you how happy and thankful I am. You got me over the finish line what’s been a challenging an arduous process, but now we can start real testing and polishing this week.

    Thanks again!

    Lisa

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get user roles in function’ is closed to new replies.