admin管理员组文章数量:1125388
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.
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.
Share Improve this question asked Feb 9, 2024 at 12:08 Rohit kcRohit kc 298 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1Looking 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
本文标签: customizationBuddyPress Edit activity function good practice
版权声明:本文标题:customization - BuddyPress Edit activity function good practice 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657084a1946283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
bp_activity_add
's first documented parameter is an ID for an existing activity to update it – Tom J Nowell ♦ Commented Feb 9, 2024 at 13:04