0

I'm creating a site that includes 2 custom role types (Doctor and Profile Admin). The doctors are able to maintain their own bios through a custom post type and restricted capabilities on their user type allowing them to only see their own bio edit page.

What Im trying to do is send an alert email to any users with the role type Profile Admin whenever a user with the role type Doctor updates their profile. The function I have now works as intended, sends an email when doctor role type users update, except right now I can only get it working when sending to a static email address. See code below:

// Send notification email when doctor updates profile
add_action( 'post_updated', 'doc_profile_change_email' );
function doc_profile_change_email ($post) {
    if ( current_user_can('doctor') ) {
        $user_info = get_userdata ($post->post_author);
        $strTo = array ('[email protected]');
        $strSubject = get_the_title($post) . ' updated their profile.';
        $strMessage = get_the_title($post) . ' submitted their profile for review at ' . wp_get_shortlink ($post->ID) . '&preview=true. Please proof.';
        wp_mail( $strTo, $strSubject, $strMessage );
    }
}

I attempted to write a function that loops through all the profile admin emails and return an array. See below:

// Get practice admin emails
function get_practice_admin_emails() {

    $profile_admin_emails = array();
    $profile_admins = get_users( $args );
    $args = array(
        'role'  =>  'profile_admin',
     ); 

    foreach ( $profile_admins as $user ) {
        $profile_admin_emails[ $user->user_email ];
    }

    return $profile_admin_emails;
}

The thought was to use this to populate the $strTo argument as follows:

$strTo = get_practice_admin_emails();

But this isn't working. I'm not sure if I'm over complicating this and there is an obvious answer I'm missing but any insight would be greatly appreciated.

1 Answer 1

3

I think you get the idea to solve it but the code is not correct.
Try this.

// Get practice admin emails
function get_practice_admin_emails() {

    $profile_admin_emails = array();
    $args = array(
        'role'  =>  'profile_admin',
     ); 
    $profile_admins = get_users( $args ); // this must be after $args

    foreach ( $profile_admins as $user ) {
        // try this instead
        $profile_admin_emails[] = $user->user_email;
    }

    return $profile_admin_emails;
}

Explanation:

  • $args must be before get_users( $args ).
  • There was no assignment in this instruction $profile_admin_emails[ $user->user_email ];.
2
  • 1
    Ah yes! I had tried $profile_admin_emails[$user->user_email] = $user but that wasnt working. Also cant believe I miss positioned my args. Thank you so much! Worked like a charm Commented Mar 14, 2018 at 17:22
  • Glad to help :-)
    – JBoulhous
    Commented Mar 14, 2018 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.