通知 $email_address 任何新答案,例如 stackoverflow

发布于 2024-10-03 20:08:10 字数 1499 浏览 8 评论 0原文

我正在研究一个允许注册用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-10-10 20:08:10

好的,我们开始吧:

在用户添加帖子的表单中,我

<input class="checkbox" type="checkbox" value="yes" name="notify" checked="checked" />

在标题中添加了“Then”

$notify = $_POST['notify'];

现在,在处理表单并将帖子插入 wpdb 的函数中

if ($notify) {
        $wpdb->insert('wp_notify', array('user_id' => $question_author, 'post_id' => $question->ID), array( '%d', '%d' ) );
    }

最后一件事是处理处理表单的函数添加评论后的评论:

$notify = $wpdb->get_col("SELECT user_id FROM wp_notify WHERE user_id = {$wp_query->post->post_author} AND post_id = {$wp_query->post->ID}");

    foreach ($notify as $user) :
        if($user == $wp_query->post->post_author && $user != $user_ID) {
            wp_mail('email', 'New Answer on Post: asdasdasdas', 'google.ro');
        }
    endforeach;

它就像魅力一样。也许有人觉得这很有用。
感谢德克的帮助。

Ok so, here we go:

In the form where you the user add the post, I added

<input class="checkbox" type="checkbox" value="yes" name="notify" checked="checked" />

Then in the header

$notify = $_POST['notify'];

Now, in the function that handles the form and insert the post into wpdb

if ($notify) {
        $wpdb->insert('wp_notify', array('user_id' => $question_author, 'post_id' => $question->ID), array( '%d', '%d' ) );
    }

And the last thing, for the function that handles the comments, after the comment is added:

$notify = $wpdb->get_col("SELECT user_id FROM wp_notify WHERE user_id = {$wp_query->post->post_author} AND post_id = {$wp_query->post->ID}");

    foreach ($notify as $user) :
        if($user == $wp_query->post->post_author && $user != $user_ID) {
            wp_mail('email', 'New Answer on Post: asdasdasdas', 'google.ro');
        }
    endforeach;

And it works like a charm. Maybe someone find this useful.
Thanks dirk for your help.

人事已非 2024-10-10 20:08:10

首先,您需要从数据库中获取该帖子的 post_author 字段。查找该作者/用户的数据库记录,从该记录中提取电子邮件,并向该电子邮件地址发送一封包含新答案通知的电子邮件。 get_userdata WordPress 函数将获取用户 ID(来自 post_author 字段)并返回一个包含用户信息的对象,包括他们的电子邮件地址。

global $post;
$user = get_userdata($post->post_author);
wp_mail($user->user_email, 'New Answer on Post: '.$post->post_title, get_permalink($post->ID));

这将获取当前帖子的作者并向他们发送一封电子邮件,主题为“帖子的新答案:[帖子名称]”,消息正文是该帖子的 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. The get_userdata WordPress function will take a user ID (from the post_author field) and return an object with information about the user, including their email address.

global $post;
$user = get_userdata($post->post_author);
wp_mail($user->user_email, 'New Answer on Post: '.$post->post_title, get_permalink($post->ID));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文