Wordpress - 在用户获得新关注者时向他们发送电子邮件

Wordpress - Send email to users when they get new followers

我正在使用一个简单的插件“user following system”,我需要做的就是在用户获得新关注者后发送一封电子邮件。

我认为这是插件代码的重要部分:

function pwuf_follow_user( $user_id, $user_to_follow ) {

    $following = pwuf_get_following( $user_id );

    if ( $following && is_array( $following ) ) {
        $following[] = $user_to_follow;
    } else {
        $following = array();
        $following[] = $user_to_follow;
    }

    // retrieve the IDs of all users who are following $user_to_follow
    $followers = pwuf_get_followers( $user_to_follow );

    if ( $followers && is_array( $followers ) ) {
        $followers[] = $user_id;
    } else {
        $followers = array();
        $followers[] = $user_id;
    }

    do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow );

    // update the IDs that this user is following
    $followed = update_user_meta( $user_id, '_pwuf_following', $following );

    // update the IDs that follow $user_id
    $followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers );

    // increase the followers count
    $followed_count = pwuf_increase_followed_by_count( $user_to_follow ) ;

    if ( $followed ) {

        do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );

        return true;
    }
    return false;
}

并在此处检查用户是否关注另一个用户:

function pwuf_is_following( $user_id, $followed_user ) {

    $following = pwuf_get_following( $user_id );

    $ret = false; // is not following by default

    if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
        $ret = true; // is following
    }

    return $ret;
}

我尝试在更新用户元数据后添加此代码,但没有任何反应!

   $subscribers = explode(",", $user_to_follow );
    $emails      = array ();

    foreach ( $subscribers as $subscriber ) {
        $user_info = get_userdata($subscriber);
        $emails[] = $user_info ->user_email;
    }
    $body = sprintf( $user_to_follow->display_name, 'followed your work! See <%s>' );

    wp_mail( $emails, 'New followers!', $body );

我相信您想在用户被另一个用户关注时向用户发送一封电子邮件。在这种情况下,插件中有这个操作挂钩可用,当以下过程成功时执行:

do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );

您可以将自己的代码挂接到此操作

add_action('pwuf_post_follow_user', $user_id, $user_to_follow) {

   $follower = get_userdata($user_id);
   $recipient = get_userdata($user_to_follow);
   $recipient_email = $recipient->user_email;
   $body = sprintf('%s followed your work!', $follower->display_name );
   wp_mail( $recipient_email , 'New follower!', $body );

}

参考:https://codex.wordpress.org/Function_Reference/get_userdata