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.
bp_activity_add
's first documented parameter is an ID for an existing activity to update it