繁体   English   中英

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

[英]Wordpress - Send email to users when they get new followers

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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