0

我处理一个允许注册用户在 Wordpress 博客上发帖的主题,我创建了一个表单(标题、类别、条目)。

问题是,如何添加一个新的复选框“发布新答案时通知我”?我需要一个函数,而不是插件。

这是处理问题发布的函数:

功能 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 功能会很棒。

4

2 回答 2

1

好的,我们开始:

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

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

然后在标题中

$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;

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

于 2010-11-24T21:13:22.840 回答
0

首先,您需要post_author从数据库中获取该帖子的字段。查找该作者/用户的数据库记录,从该记录中提取电子邮件,然后向该电子邮件地址发送一封包含新答案通知的电子邮件。get_userdataWordPress 函数将获取一个用户 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。

于 2010-11-24T16:06:53.757 回答