0

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

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

4

1 回答 1

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');
于 2012-12-07T09:39:21.990 回答