dqjjw04440 2018-10-01 06:27 采纳率: 0%
浏览 59
已采纳

Wordpress - 当其他用户喜欢他们的帖子时向用户发送电子邮件

I'm using " simple like system " Plugin, I have this function to check when users get post liked by the other users, How can i send an email to post author when his posts got liked by the others?

Something like " Hey {Post Author name}, {username of who likes the post} liked your post {post title}" { Post permalink}, visit {username} Profile at {author url}.

/**
 * Utility to test if the post is already liked
 * @since    0.5
 */
function already_liked( $post_id, $is_comment ) {
    $post_users = NULL;
    $user_id = NULL;
    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id();
        $post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_liked" ) : get_post_meta( $post_id, "_user_liked" );
        if ( count( $post_meta_users ) != 0 ) {
            $post_users = $post_meta_users[0];
        }
    } else { // user is anonymous
        $user_id = sl_get_ip();
        $post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_IP" ) : get_post_meta( $post_id, "_user_IP" ); 
        if ( count( $post_meta_users ) != 0 ) { // meta exists, set up values
            $post_users = $post_meta_users[0];
        }
    }

    if ( is_array( $post_users ) && in_array( $user_id, $post_users ) ) {
        return true;
    } else {
        return false;
    }
} // already_liked()

this one for post user likes

/**
 * Utility retrieves post meta user likes (user id array), 
 * then adds new user id to retrieved array
 * @since    0.5
 */
function post_user_likes( $user_id, $post_id, $is_comment ) {
    $post_users = '';
    $post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_liked" ) : get_post_meta( $post_id, "_user_liked" );
    if ( count( $post_meta_users ) != 0 ) {
        $post_users = $post_meta_users[0];
    }
    if ( !is_array( $post_users ) ) {
        $post_users = array();
    }
    if ( !in_array( $user_id, $post_users ) ) {
        $post_users['user-' . $user_id] = $user_id;
    }
    return $post_users;
} // post_user_likes()

Here is what i tried to do but the whole function stopped working and email not sent.

I added do_action and add_action, I don't know why but i saw another snippet did that.

Original function above!!

function already_liked( $post_id, $is_comment ) {
    $post_users = NULL;
    $user_id = NULL;
    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id();
        $post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_liked" ) : get_post_meta( $post_id, "_user_liked" );
        if ( count( $post_meta_users ) != 0 ) {
            $post_users = $post_meta_users[0];
        }
    } else { // user is anonymous
        $user_id = sl_get_ip();
        $post_meta_users = ( $is_comment == 1 ) ? get_comment_meta( $post_id, "_user_comment_IP" ) : get_post_meta( $post_id, "_user_IP" ); 
        if ( count( $post_meta_users ) != 0 ) { // meta exists, set up values
            $post_users = $post_meta_users[0];
        }
    }

    $user = get_userdata($user_id);
    $user_url = get_author_posts_url( $user_id );
    $recipient = get_userdata($post_users);
    $recipient_email = $recipient->user_email;
    $body = sprintf('%s Liked your post!'. "

", $user->display_name );
    $body .= sprintf( 'Visit Artist Page: %s', $user_url );
    wp_mail( $recipient_email , 'New Likes!', $body );

    add_action('post_like_user', $user_id, $post_users );

    if ( is_array( $post_users ) && in_array( $user_id, $post_users ) ) {

            do_action( 'post_like_user', $user_id, $post_users );

        return true;

    } else {
        return false;
    }
} // already_liked()
  • 写回答

1条回答 默认 最新

  • drp935159 2018-10-01 06:53
    关注

    According to the fact that it looks like that you are using Wordpress, I can advise you to use the default e-mail tool in Wordpress.

    wp_mail( string|array $to, string $subject, string $message, string|array $headers = '', string|array $attachments = array() );
    

    Reference: (Developer Resources) https://developer.wordpress.org/reference/functions/wp_mail/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle