Search code examples
phpwordpressemailauthor

Wordpress - Send email to users when they get new followers


I'm using a simple plugin "user following system" And all what i need to do is sending an email after the users get a new followers.

I think this is the important part of the plugin code:

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

and here to check if a user is following another:

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

I tried to add this code after updating the user meta but nothing happen!

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

Solution

  • I believe you want to send user an email when ever user is been followed by another user. Given this scenario, there is this action hook available in the plugin which is executed when following process is successful:

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

    You can hook your own code to this action

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

    Refernece: https://codex.wordpress.org/Function_Reference/get_userdata