wordpress收费下载资源主题
wordpress付费下载资源插件

WordPress函数wp_notify_postauthor详细用法教程解析

wp_notify_postauthor函数是WordPress中用于通知文章作者的函数。该函数会在有新的评论发表或有评论被批准时自动调用,发送邮件给文章作者。

函数的定义如下:

wp_notify_postauthor( $comment_id, $comment_type = '' )

参数说明:
- $comment_id (int):评论的ID。
- $comment_type (string):评论的类型,默认为空字符串。

函数的具体用法如下:

function custom_notify_postauthor( $comment_id, $comment_type = '' ) {
$comment = get_comment( $comment_id ); // 获取评论对象

// 获取文章对象
$post = get_post( $comment->comment_post_ID );

// 获取文章作者的用户对象
$author = get_userdata( $post->post_author );

// 邮件主题
$subject = sprintf(
__( '[%1$s] Comment: "%2$s"' ),
$post->post_title,
$comment->comment_content
);

// 邮件内容
$message = sprintf(
__( 'A new comment on the post "%1$s" is waiting for your approval.' ),
$post->post_title
) . "rnrn";
$message .= sprintf(
__( 'Author: %1$s (IP address: %2$s, %3$s)' ),
$comment->comment_author,
$comment->comment_author_IP,
$comment->comment_author_url
) . "rnrn";
$message .= sprintf(
__( 'Email: %s' ),
$comment->comment_author_email
) . "rnrn";
$message .= sprintf(
__( 'URL: %s' ),
$comment->comment_author_url
) . "rnrn";
$message .= sprintf(
__( 'Comment: %s' ),
$comment->comment_content
) . "rnrn";
$message .= __( 'You can see all comments on this post here: ' ) . "rn";
$message .= get_permalink( $comment->comment_post_ID ) . "#commentsrnrn";
$message .= sprintf(
__( 'Permalink: %s' ),
get_comment_link( $comment_id )
);

// 发送邮件给文章作者
wp_mail( $author->user_email, $subject, $message );
}
add_action( 'wp_insert_comment', 'custom_notify_postauthor' ); // 在评论插入时调用函数

上述代码是一个自定义的通知文章作者函数,它会在评论被插入时发送邮件给文章作者。你可以把这段代码放在主题的functions.php文件中。

注意:该函数会在评论插入时调用,所以对于已有的评论,该函数不会发送邮件通知。

希望以上解析能够满足你的需求,如果有更多问题,请随时提问。

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情