Allow activity authors to delete activity comments by other users on your BuddyPress based social network
Last week, Chaitanya suggested me to write a plugin that will allow activity authors to delete any comment they receive on their activity. Since it can be done in just couple of lines, I thought I will go with a tutorial.
Problem: Currently BuddyPress does not allow the author of an activity to delete comments they receive on their activity( Site Admins can delete anything though ).
To fix this, all we needed was filter on "bp_activity_user_can_delete" and return true when the current logged in user is author of the parent activity.
Here is the code that does that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * Filters delete permission. * * @param bool $can_delete can delete. * @param BP_Activity_Activity $activity activity object. * * @return bool */ function buddydev_activity_user_can_delete( $can_delete, $activity ) { // if the user already has permission // or the user is not logged in, we don't care. if ( $can_delete || ! is_user_logged_in() ) { return $can_delete; } // if we are here, let us check if the current user is the author of the parent activity. $parent_activity = null; // if it is an activity comment. if ( $activity->item_id ) { $parent_activity = new BP_Activity_Activity( $activity->item_id ); } else { $parent_activity = $activity; } // if the current user is author of main activity, he/she can delete it. if ( $parent_activity->user_id == get_current_user_id() ) { $can_delete = true; } return $can_delete; } add_filter( 'bp_activity_user_can_delete', 'buddydev_activity_user_can_delete', 10, 2 ); |
Now, as you see, we have granted the author of the activity the permission to delete any comment. That works but there is a catch.
Currently, when the activity comments are deleted via ajax request, the "bp_activity_user_can_delete" function is not called. So, we need to account for that case too.
The code below is a modified code from bp default theme which calls the function "bp_activity_user_can_delete" to check for the permission instead of checking for bp_moderate permission.
The one thing that you should notice is, we have increased the priority of our attached callback. So, our function gets called first and gets the chance to handle the request first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * Custom delete activity comment handler. */ function buddydev_ajax_delete_activity_comment() { // Bail if not a POST action. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { return; } // Check the nonce. check_admin_referer( 'bp_activity_delete_link' ); if ( ! is_user_logged_in() ) { exit( '-1' ); } $comment = new BP_Activity_Activity( $_POST['id'] ); if ( ! bp_activity_user_can_delete( $comment ) ) { return false; } // let others handle it. // Call the action before the delete so plugins can still fetch information about it. do_action( 'bp_activity_before_action_delete_activity', $_POST['id'], $comment->user_id ); if ( ! bp_activity_delete_comment( $comment->item_id, $comment->id ) ) { exit( '-1<div id="message" class="error"><p>' . __( 'There was a problem when deleting. Please try again.', 'buddypress' ) . '</p></div>' ); } do_action( 'bp_activity_action_delete_activity', $_POST['id'], $comment->user_id ); exit; } // hook to ajax comment delete action. // we have increased our priority to make sure that our handler gets called first. add_action( 'wp_ajax_delete_activity_comment', 'buddydev_ajax_delete_activity_comment', 0 ); |
You can put the above code in bp-custom.php(in plugins directory)ย orย in your theme's functions.php.
Well, I hope it helps you. Please do let me know our views/suggestion or any other feedback in the comment section.
Thanks a lot Brajesh!
The code works like a charm.
That was exactly what I was looking for. Thanks!
Chaitanya
Thank you for confirming Chaitanya.
I am glad it is working ๐
This can get useful ๐
Hi Brajesh! I want post image in comment of buddypress. How to do? ๐
HI Nguyen,
Do you want to allow posting images in the activity comment or blog comment?
There is a plugin for blog comment
https://wordpress.org/plugins/comment-images/
For activity comment, there does not seem to be any option at the moment.
Hello, thank you for awesome tip.
Please,
can you create the same thing, but this time to
Allow activity authors to delete their own comments and comments by other users on their blog posts?
Thank you very much ๐
Hi Mike,
Thank you for the comment.
It's a good idea and can be done. I have a question though:- what happens to the deleted comment? Should we delete it completely or just remove the association?
Sorry, I missed your reply ๐
I think to start with just a possibility let users to delete their own comments would be great.
Thanks a lot :))
I already replied few times Brajesh, but I can't see it here.
Please, if you know easy way to allow users to delete their comment, please let me know… we know there are some plugins allowing it, but we need simple delete button… ideally without the pop-up question "Are you sure?" because this popup doesn't show in Web View on iOS.
Thanks a lot ๐
Hi Mike,
Sorry about that.
You don't need any plugin for that. Please look into your theme/buddypress/activity/comment.php and you will see a line like this
All you need to do is to remove the "confirm" class from it and it will work.
Regards
Brajesh