如何让WordPress用户角色显示在新用户通知电子邮件中

时间:2017-01-30 19:10:54

标签: php wordpress

我正在尝试将wordpress新用户通知电子邮件修改为admin,使用其他人在此处共享的一些代码。这是我添加到它的内容,但在新用户通知电子邮件中显示:“Role:Array”而不是角色值。关于如何让它显示角色价值的任何建议?

<?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);

}
}

?>

1 个答案:

答案 0 :(得分:0)

仅供参考,我想出来并决定发布作为答案,以防有人有兴趣将此添加到新用户通知电子邮件(仅限管理员):

<?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;

    }
}

?>