BuddyPress,在发布新故事时发送通知

时间:2012-12-06 21:29:24

标签: php mysql wordpress buddypress

我已使用自定义配置文件字段设置BuddyPress,该字段列出了与我的网站相关的标签的复选框。

有没有办法在新帖发布时根据自定义配置文件字段选择向已注册的BuddyPress用户发送自动通知?

1 个答案:

答案 0 :(得分:1)

save_post挂钩可以帮助你。

http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

有些事情:

function send_bp_message( $post_id ) 
{
  //verify post is not a revision
  if( wp_is_post_revision($post_id) )
  {
    return;
  }

  // get the user ids you want to notify
  global $bp, $wpdb;

  $custom_field_id = 1; // the profile field you want to check
  $custom_field_value = 'true'; // the value you're looking for

  $stmt = $wpdb->prepare("
    SELECT
      {$bp->profile->table_name_data}.user_id
    FROM
      {$bp->profile->table_name_data}
    LEFT JOIN
      {$bp->profile->table_name_fields} ON {$bp->profile->table_name_fields}.id = {$bp->profile->table_name_data}.field_id
    WHERE
      {$bp->profile->table_name_fields}.id = %d
    AND
      {$bp->profile->table_name_data}.value LIKE %s
  ", $custom_field_id, $custom_field_value);

  $recipient_ids = $wpdb->get_col($stmt); // array of matched user ids

  // send buddypress notification to matched user ids
  // (you could loop through $recipient_ids to send individual notifications)
  $msg_args = array(
    'sender_id' => 1, // 1 = admin
    'recipients' => $recipient_ids,
    'subject' => 'New post',
    'content' => 'A new post has been created...'
  );
  $thread_id = messages_new_message($message_args);        
}
add_action('save_post', 'send_bp_message');
相关问题