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

每当有新帖子发表时,是否可以根据他们的自定义配置文件字段选择向注册的BuddyPress用户发送自动通知?

最佳答案

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');

10-06 04:09