通知 $email_address 任何新答案,例如 stackoverflow
我正在研究一个允许注册用户在 WordPress 博客上发帖的主题,我创建了一个表单(标题、类别、条目)。
问题是,如何添加新的复选框“发布新答案时通知我”?我需要一个功能,而不是插件。
这是处理问题发布的函数:
function post_new_question($question_title, $question_content, $question_category) {
$question_title_stripped = strip_tags($question_title);
$question_content_stripped = strip_tags($question_content);
$user = wp_get_current_user();
global $wpdb;
$gather_questions = "SELECT * FROM wp_posts WHERE post_author = '" . $user->ID . "'";
$user_questions = $wpdb->get_results($gather_questions);
if (isEmptyString($question_title_stripped)) return new WP_Error('no_title_entered', 'Enter a title for your quesion');
if (isEmptyString($question_content_stripped)) return new WP_Error('no_content', 'Enter a breif description for your quesion');
foreach ($user_questions as $user_question) {
if ($user_question->post_author == $user->ID ) {
if ($user_question->post_title == $question_title_stripped) {
return new WP_Error('duplicate_user_question', 'You have already asked this exact question.');
} else {}
} else {}
}
$question_author = $user->ID;
$post = array(
'ID' => '',
'post_author' => $question_author,
'post_category' => array($question_category),
'post_content' => $question_content_stripped,
'post_title' => $question_title_stripped,
'post_status' => 'publish'
);
$question_id = wp_insert_post($post); }
PS: 使用 wp_email 函数会很棒。
I work on a theme that allows Registered Users to post on a Wordpress Blog, I created a form (title, category, entry).
The question is, how can I add a new checkbox "Notify me when new answer is posted"? I need a function, not a plugin.
Here is the function that handles the question posting:
function post_new_question($question_title, $question_content, $question_category) {
$question_title_stripped = strip_tags($question_title);
$question_content_stripped = strip_tags($question_content);
$user = wp_get_current_user();
global $wpdb;
$gather_questions = "SELECT * FROM wp_posts WHERE post_author = '" . $user->ID . "'";
$user_questions = $wpdb->get_results($gather_questions);
if (isEmptyString($question_title_stripped)) return new WP_Error('no_title_entered', 'Enter a title for your quesion');
if (isEmptyString($question_content_stripped)) return new WP_Error('no_content', 'Enter a breif description for your quesion');
foreach ($user_questions as $user_question) {
if ($user_question->post_author == $user->ID ) {
if ($user_question->post_title == $question_title_stripped) {
return new WP_Error('duplicate_user_question', 'You have already asked this exact question.');
} else {}
} else {}
}
$question_author = $user->ID;
$post = array(
'ID' => '',
'post_author' => $question_author,
'post_category' => array($question_category),
'post_content' => $question_content_stripped,
'post_title' => $question_title_stripped,
'post_status' => 'publish'
);
$question_id = wp_insert_post($post); }
PS: Usage of wp_email function would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我们开始吧:
在用户添加帖子的表单中,我
在标题中添加了“Then”
现在,在处理表单并将帖子插入 wpdb 的函数中
最后一件事是处理处理表单的函数添加评论后的评论:
它就像魅力一样。也许有人觉得这很有用。
感谢德克的帮助。
Ok so, here we go:
In the form where you the user add the post, I added
Then in the header
Now, in the function that handles the form and insert the post into wpdb
And the last thing, for the function that handles the comments, after the comment is added:
And it works like a charm. Maybe someone find this useful.
Thanks dirk for your help.
首先,您需要从数据库中获取该帖子的
post_author
字段。查找该作者/用户的数据库记录,从该记录中提取电子邮件,并向该电子邮件地址发送一封包含新答案通知的电子邮件。get_userdata
WordPress 函数将获取用户 ID(来自post_author
字段)并返回一个包含用户信息的对象,包括他们的电子邮件地址。这将获取当前帖子的作者并向他们发送一封电子邮件,主题为“帖子的新答案:[帖子名称]”,消息正文是该帖子的 URI。
First you need to grab the
post_author
field from the database for that post. Look up the database record for that author/user, pull the email out of that record, and send an email with a notification of a new answer to that email address. Theget_userdata
WordPress function will take a user ID (from thepost_author
field) and return an object with information about the user, including their email address.That will grab the author of the current post and send them an email with the subject "New Answer on Post: [Name of Post]" and the message body being the URI for the post.