0

I am using a custom BuddyPress edit function. However, I was told this is a bad practice and that I should use WordPress's helper function. How to correctly do this? Here, is the code:

add_action('wp_ajax_update_contest_entry', 'update_contest_entry');
add_action('wp_ajax_nopriv_update_contest_entry', 'update_contest_entry');

function update_contest_entry() {
  global $wpdb;
    $entry_id = isset($_POST['entry_id']) ? intval($_POST['entry_id']) : 0;
    $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : '';
    $content = isset($_POST['body']) ? wp_kses_post($_POST['body']) : '';
    $signature = isset($_POST['signature']) ? sanitize_text_field($_POST['signature']) : '' ;
    $contentType = isset($_POST['contentType']) ? sanitize_text_field($_POST['contentType']) : '';

  if($contentType !== 'activity_comment'){
    $updated_activity = $wpdb->update(
      $wpdb->base_prefix . 'bp_activity',
      array(
          'content' => $content
      ),
      array('id' => $entry_id),
      array('%s'),
      array('%d')
    );
         $updated_meta = $wpdb->update(
          $wpdb->base_prefix . 'bp_activity_meta',
          array('meta_value' => $title),
          array('activity_id' => $entry_id, 'meta_key' => 'title'),
          array('%s'),
          array('%d', '%s')
      );

      $updated_meta_signature = $wpdb->update(
          $wpdb->base_prefix . 'bp_activity_meta',
          array('meta_value' => $signature),
          array('activity_id' => $entry_id, 'meta_key' => 'signature'),
          array('%s'),
          array('%d', '%s')
      );

      if ($updated_activity !== false && $updated_meta !== false && $updated_meta_signature !== false)  {
        wp_send_json_success('Contest entry updated successfully.');
    } else {
        wp_send_json_error('Error updating contest entry.');
    }
  }
  }

The feedback was that directly modifying the database is a bad practice. Use helper functions to update posts/meta in WordPress. But I didn't find any Buddypress/WordPress function to update activities.

6
  • have you looked at codex.buddypress.org/developer/function-examples/… and the other BP functions? bp_activity_add's first documented parameter is an ID for an existing activity to update it
    – Tom J Nowell
    Commented Feb 9 at 13:04
  • I actually want to edit the posts like (activity_update, activity_comment) from the activity stream page.
    – Rohit kc
    Commented Feb 9 at 13:08
  • I don't see how that would make a difference to my comment, did you check the source code of the BP Activity folder?
    – Tom J Nowell
    Commented Feb 9 at 13:10
  • Oh ok, got it. So, by passing the id of the existing post. I can use bp_activity_add() to update the posts. It's great.
    – Rohit kc
    Commented Feb 9 at 13:12
  • 1
    I left an answer, btw if you're relying on JSON for your AJAX you might want to consider using a REST API endpoint, that way you only need to return data and WP will JSON encode it for you, as well as giving you a pretty URL. Buddypress might already have endpoints you can interact with from JS that would let you bypass this entirely
    – Tom J Nowell
    Commented Feb 9 at 13:17

1 Answer 1

1

Looking at https://buddypress.trac.wordpress.org/browser/trunk/src/bp-activity/bp-activity-functions.php there's several functions:

Activity Meta

I found bp_activity_add_meta, bp_activity_delete_meta and bp_activity_update_meta in that file, e.g. bp_activity_update_meta:

        // Update activity meta counts.
        if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {

Activities

It looks like these can be updated the same way wp_insert_post can be used to update a post, by calling bp_activity_add with the updated information, and giving it an ID of the activity you want it to update

Activity Comments

That file also includes all comment related functions, but it looks like bp_activity_new_comment works the same way as wp_insert_post in that you can modify a comment by passing its ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.