简体   繁体   中英

How to get WordPress user role to show in new user notification email

I'm trying to modify the wordpress new user notification email to admin, using a bit of code someone else shared on here. Here's what I have added to it, but in the new user notification email it displays: "Role: Array" instead of the role value. Any suggestions on how to get it to show the role value?

<?php
/*
Plugin Name: Custom New User Email
Description: Changes the copy in the email sent out to new users
*/
// Redefine user notification function
if ( !function_exists('wp_new_user_notification') ) {
function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
    $user = new WP_User($user_id);

    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);
    $user_info = get_userdata(1);

    $message  = sprintf(__('New user registration on %s:'), get_option('blogname')) . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n\r\n";
    $message .= sprintf(__('Role: %s'), $user_info->roles) . "\n";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);

}
}

?>

FYI, I figured it out and decided to post as an answer in case anyone is interested in adding this to the new user notification email (to admin only):

<?php
/*
Plugin Name: Custom New User Email
Description: Changes the copy in the email sent out to new users
*/
// Redefine user notification function
if ( !function_exists('wp_new_user_notification') ) {
    function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
    $user = new WP_User($user_id);

    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);
    $user_meta = get_userdata($user_id);
    $user_roles = $user_meta->roles;

    $message  = sprintf(__('New user registration on %s:'), get_option('blogname')) . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n\r\n";
    $message .= sprintf(__('Role: %s'), implode(', ',$user_meta->roles)) . "\n";


    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);

    if ( empty($plaintext_pass) )
        return;

    }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM