如何在 buddypress 中添加、保存和循环自定义 activity 字段?

How to add, save and loop over custom activity fields in buddypress?

请帮忙,我正在尝试将一些自定义字段添加到 buddypress 中的 activity 流,但卡住了...

以下代码成功将两个字段添加到 activity 表单中:

//add fields to activity
function add_activity_extra_fields(){
  echo
  '<div id="activitty-form-extra-fields">
    <input type="url" data-clear-btn="false" name="activity-form-field-url" id="activity-form-field-url" value=""   placeholder="Insert link...">  
    <input type="date" name="activity-form-field-date" id="activity-form-field-date" value="" placeholder="Insert expiry.. ."/>
  </div>';
} add_action ( "bp_activity_post_form_options", 'add_activity_extra_fields' );

然后我可以从 $_POST 读取并保存到自定义 table,但它不起作用:

//save fields from activity
function save_activity_extra_fields( $content ) {
  global $wpdb;
  
  //$act_id = $_POST['activity-form-field-date'];
  $usr_id = $_POST['user_id'];
  $date = $_POST['activity-form-field-date'];
  $url = $_POST['activity-form-field-url'];

  $wpdb->insert(
    'jrh_bp_activity_extra',
    array( 'user_id' => $usr_id, 'date' => $date, 'url' => $url ),
    array( '%s' ),
  );

} add_action('bp_activity_posted_update', 'save_activity_extra_fields');

这个钩子 bp_activity_posted_update 不是最好的方法。改用这个钩子:bp_activity_after_save

你应该把它存储在这个 table: $wpdb->prefix . 'bp_activity_meta'; 除非你真的想使用自定义 table.

并且您的 $wpdb->insert 查询格式不正确。您应该为数据中的每个值设置一种格式。 See codex.

我将字段添加到 activity 表单中,如下所示:

function add_activity_extra_fields(){
  echo
  '<div id="activitty-form-extra-fields">
    <input type="url" data-clear-btn="false" name="activity-form-field-url" id="activity-form-field-url" value=""   placeholder="Inserisci link...">  
    <input type="date" name="activity-form-field-date" id="activity-form-field-date" value="" placeholder="Inserisci scadenza.. ."/>
  </div>';
} add_action ( "bp_activity_post_form_options", 'add_activity_extra_fields' );

然后保存到自定义table:

function save_activity_extra_fields( $activity ) {
  global $wpdb;
  
  $activity_id = $activity -> id;
  $url = $_POST['activity-form-field-url'];
  $date = $_POST['activity-form-field-date'];
  
  $save = $wpdb->insert(
    'jrh_bp_activity_extra',
    array( 'activity_id' => $activity_id, 'date' => $date, 'url' => $url ),
    array( '%d', '%s', '%s' ),
  );

} add_action('bp_activity_after_save', 'save_activity_extra_fields');

最后用下面的函数检索...

function read_activity_extra_fields() {
  global $activities_template;
  global $wpdb;
  
  $id = $activities_template->activity->id;  
  
  $url = $wpdb->get_results( "SELECT url FROM `jrh_bp_activity_extra` WHERE activity_id = $id" );
  $date = $wpdb->get_results( "SELECT date FROM `jrh_bp_activity_extra` WHERE activity_id = $id" );
  
  $extra_fields = (object) array( 'url' => $url[0]->url, 'date' => $date[0]->date);
  
  return $extra_fields;
}

不幸的是我找不到更好的方法,所以我只是从我的自定义 entry.php 模板中调用它

<div class="activity-inner-wish-extra">
    <?php 
        $extra_fields = read_activity_extra_fields();

        if ( isset($extra_fields->url) ) :
            echo '<p class="wish-url">'; echo $extra_fields->url; echo '</p>';
        endif;
        
        if ( isset($extra_fields->date) ) :
            echo '<p class="wish-expiry">'; echo $extra_fields->date; echo '</p>';
        endif;
    ?>
</div>