WordPress-当用户的帖子被其他用户喜欢时给用户发送电子邮件

时间:2018-10-01 06:27:18

标签: php wordpress email author

我正在使用“ simple like system插件,我具有此功能来检查用户何时获得其他用户的喜欢的帖子,当他的帖子被其他用户喜欢时,如何发送电子邮件给帖子作者?

类似“嘿{帖子作者姓名},{喜欢该帖子的用户名}喜欢您的帖子{帖子标题}” {帖子永久链接},请访问{username}个人资料,网址为{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()

该帖子供邮政用户喜欢

/**
 * 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()

这是我尝试执行的操作,但整个功能停止工作且未发送电子邮件。

我添加了do_action和add_action,我不知道为什么,但是我看到了另一个代码片段。

上面的原始功能!

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!'. "\n\n", $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 个答案:

答案 0 :(得分:0)

根据您似乎正在使用Wordpress的事实,我建议您使用Wordpress中的默认电子邮件工具。

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

参考:(开发人员资源)https://developer.wordpress.org/reference/functions/wp_mail/