有条件地发送用户角色升级确认

时间:2012-05-02 作者:Pollux Khafra

当用户的帐户升级到新的用户角色时,我使用这段代码向用户发送确认电子邮件。

function user_role_update( $user_id, $new_role ) {
    $site_url = get_bloginfo(\'wpurl\');
    $user_info = get_userdata( $user_id );
    $to = $user_info->user_email;
    $subject = "Role changed: ".$site_url."";
    $message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
    wp_mail($to, $subject, $message);
}
add_action( \'set_user_role\', \'user_role_update\', 10, 2);
无论账户是升级还是降级,这都会发送一封电子邮件,这并不能让我灵活地发送不同的降级消息。我有两种情况使用这个。

当用户从“贡献者”升级到“作者”时如何使用条件为每个事件发送不同的电子邮件?

1 个回复
最合适的回答,由SO网友:Adam 整理而成

很可能像这样的事情会起作用;

    function user_role_update( $user_id, $new_role ) {
    $site_url = get_bloginfo(\'wpurl\');
    $user_info = get_userdata( $user_id );

    if (user_can( $user_id, \'capability\' ) ) {

        $to = $user_info->user_email;
        $subject = "Role changed: ".$site_url."";
        $message = "Hello " .$user_info->display_name . " your role has changed on     ".$site_url.", congratulations you are now an " . $new_role;
        wp_mail($to, $subject, $message);

    } elseif (user_can( $user_id, \'capability\' ) ) {

    // etc...

    }

}
add_action( \'set_user_role\', \'user_role_update\', 10, 2);

结束