例如,我写了一篇新的帖子,内容是“今天我很高兴,因为我终于遇到了那个@David告诉我的女孩……”这篇文章发表后,应该会向David发送一封关于这篇提到他的文章的电子邮件通知。已经有这样的插件了吗?如果没有,请帮助我实施。非常感谢!:)
脚本需要首先解析这篇新帖子的内容。在“@”后查找姓名,然后转到数据库以匹配评论者的姓名,以获取他/她的电子邮件地址(假设我@之前必须在我的博客上发表评论的人)
下面是发送电子邮件的基本部分。
function email_friend() {
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = \'David@email.com\';
$subject = \'Arch!tect mentioned you in his new post《\'.$postTitle .\'》\';
$from = "noreplay@myWebsite.com";
$headers = "From:" . $from;
$message = "Arch!tect mentioned you in his new post《".$postTitle .
"》 check it out?\\n\\n" ."Post link:".$post_permalink
."\\n\\n\\nPlease don\'t reply this email.\\r\\n";
mail($to, $subject, $message, $headers);
}
add_action ( \'publish post\', \'email_friend\' );
完成简单部分!
现在请帮我解决困难的部分。。。解析post中的名称并获取数据库中的电子邮件地址。
<小时>P.S。
谢谢大家的帮助!我成功地完成了代码,现在运行良好(已测试)。为了提及名字中有空格的人,我先使用下划线,然后再将其改回空格。现在,它会向我在帖子中提到的所有人发送电子邮件,完成后,它会向我自己发送一份结果摘要。我将粘贴下面的完整代码,请看一看,并告诉我需要改进的地方。
function email_friend() {
// get post object
$post = get_post($id);
// get post content
$content = $post->post_content;
// get how many people is mentioned in this post
//$mentionCount = preg_match_all(\'/(@(\\w)+)/\', $content, $matches);
$mentionCount = preg_match_all(\'/(@[^\\s]+)/\', $content, $matches);//support other lang
// if there is at least one @ with a name after it
if (0 !== $mentionCount) {
$friendList = array();//for storing correct names
for ($mentionIndex=0; $mentionIndex < $mentionCount; $mentionIndex++) {
$mentionName = $matches[0][$mentionIndex];
$mentionName = str_replace(\'_\',\' \',$mentionName); //change _ back to space
$mentionName = substr($mentionName, 1); //get rid of @
//for security and add wildcard
$friend_display_name_like = \'%\' . like_escape($mentionName) . \'%\';
global $wpdb;
// get correct name first
$friendCorrectName = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friend_display_name_like
)) ;
// get friend email by comment author name
$friend_email = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author_email
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friendCorrectName
)) ;
if($friend_email) {// if found email address then email
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = $friend_email;
$subject = \'Arch!tect mentioned you in his new post 《\'.$postTitle .
\'》\';
$from = "noreplay@swotong.com";
$headers = "From:" . $from;
$message = "Arch!tect mentioned you in his new post《".$postTitle .
"》 check it out?\\n\\n" ."Post link:".$post_permalink
."\\n\\n\\nPlease don\'t reply this email.\\r\\n";
if(mail($to, $subject, $message, $headers)) {
//if send successfully put his/her name in my list
array_push($friendList, $friendCorrectName);
//array_push($friendList, $friend_email);
}
}
}
$comma_separated = implode(",", $friendList); //friend list array to string
// now send an email to myself about the result
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = \'myOwn@email.com\';
$subject = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully";
$from = "noreplay@email.com";
$headers = "From:" . $from;
//list all friends that received my email
$message = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully:\\n\\n".$comma_separated ;
mail($to, $subject, $message, $headers);
}
}//end of email_friend function
add_action ( \'publish_post\', \'email_friend\' );
编辑:已将(\\w)+更改为[^\\s]+,因此它支持更多语言。(中文测试)
最合适的回答,由SO网友:Max Yudin 整理而成
根据凯撒的建议进行了两次编辑,删除了不安全的版本。
<?php
function my_email_friend($post_id = \'\') {
// get post object
$post = get_post($post_id);
// get post content
$content = $post->post_content;
// if @David exists
if( 0 !== preg_match(\'/(@\\w)/\', $content, $matches) )
$friend_display_name_like = \'%\' . like_escape($matches[0]) . \'%\';
else
return; // do nothing if no matches
global $wpdb;
// get friend email by \'display_name\'
$friend_email = $wpdb->get_var( $wpdb->prepare( "
SELECT user_email
FROM $wpdb->users
WHERE display_name
LIKE %s ",
$friend_display_name_like
)) ;
if($friend_email) {
/* Your code here, \'mail()\' can use \'$friend_email\' */
}
}
add_action(\'publish_post\', \'my_email_friend\');
仅将其用作起点,因为:
代码已部分测试(读取:未测试)你可以在帖子中提到很多朋友,第一个提到的会被通知(可以增强)你可以有email@example.com在帖子内容中。脚本将尝试查找“示例”用户只有一个John
从两者John Doe
和John Roe
将通过电子邮件发送所有功能都需要增强,但似乎已经准备好进行测试。