2010-11-24 6 views
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機能の使い方は素晴らしいことです。

答えて

1

[OK]をので、ここで行く:

ユーザーの投稿を追加する形で、私は、ヘッダに続いて

で今
$notify = $_POST['notify']; 

を、

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

を追加しましたフォームを処理し、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; 

は、そして、それは魔法のように動作します。おそらく、誰かがこれを役に立つと思うかもしれない。 ありがとうダークあなたの助けになります。

0

まず、その投稿のデータベースから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ています。

+0

ありがとうございます。ユーザーが通知を希望するかどうかをチェックしたかどうかを確認する方法はありますか?そして、それはその特定の投稿について新しい答えを探す場所はどこですか? –

+1

通知の場合は、データベース内のポストメタまたはカスタムテーブルを使用してそのプリファレンスを保存する必要があります。コメントシステムを使用してコメントを回答として使用し、新しいコメントが投稿されたときに機能を実行させて、ユーザーが電子メールを受信するかどうかをチェックし、電子メールを送信したかどうかを確認することができます。 – dirk

+0

実際には、回答のように行動します。新しいテーブルwp_notifyを作成し、user_idとpost_idを保存します(通知を受けたい場合)。しかし、新しい回答がその投稿に掲載されているかどうかを確認する方法の例が必要です。ありがとう。 –

関連する問題