Skip to:
Content
Pages
Categories
Search
Top
Bottom

edit_comment hook in bp-blogs/bp-blogs-functions.php causing error

  • Profile photo of wspencer
    wspencer
    Participant

    @wspencer

    I’m running into an issue with a custom comment editor I created. I’ve tracked the issue to the bp-blogs/bp-blogs-functions.php file where it calls the bp_blogs_record_comment() function. It seems this function is overwriting my updated comments when I use my custom editor and keeping the original comment. I’m not sure what’s going on here but when I comment out the add_action hook for this function, my custom comment editor works like a charm. I’m not sure if this has something to do with some of the other custom BP functions that are called inside of the problem function or what. I’m relatively new to BP so any help is appreciated. For now, I’ve just commented out the add_action hook. Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Profile photo of danbp
    danbp
    Moderator

    @danbp

    Can you pastebin the code you are using, the theme name and the file name of your custom change ? Thx

    Profile photo of wspencer
    wspencer
    Participant

    @wspencer

    I’m not using a purchased theme. It’s a custom WP theme I created. There is also no BuddyPress theme, so it’s using bp-default. I only use BuddyPress to set up some core features I was looking to introduce. Here is the code from my custom comment editor. Please note that all data from $_POST checks out and is passing properly to the function (the variables $comment_ID and $comment_content are populated via the $_POST object. The wp_update_comment() function returns 1, which means it was successful.

    $commentarr = array();
     $commentarr['comment_ID'] = $comment_ID;
     $commentarr['comment_content'] = $commentContent;
     $update_success = wp_update_comment($commentarr);
    
     if ($update_success == 1) {
         $comment = get_comment($comment_ID);
    
         $article_link = get_permalink($comment->comment_post_ID);
    
         wp_redirect( $article_link );
      }
    
      else {
         $commentError = 'Something went wrong while updating your comment.';
         $hasError = true;
      }

    Then digging into the core, here’s where wp_update_comment() function is called….

    function wp_update_comment($commentarr) {
    ...
    	do_action('edit_comment', $comment_ID);
    ...
    }

    I tested this entire function and everything works fine except for do_action(‘edit_comment’, $comment_ID), which of course is only a hook for other plugins, etc to attach to. If I commented out the do_action, my comments are updated properly.

    So then I started turning off all plugins and trying again. Turns out it was BuddyPress causing the issue. After sorting through the BP code, I found that in the file bp-blogs/bp-blogs-functions.php, the function that is added to the ‘edit_comment’ hook is called bp_blogs_record_comment(), which is found around line 542. This function has some BP specific functions being called within it, which I’m not familiar with. At the end of the day, I’ve found that if I comment out the action hook in the last line, the comment editing works 100%. Here’s the code from bp_blogs_record_comment()

    function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
    ...
    }
    add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    //add_action( 'edit_comment', 'bp_blogs_record_comment', 10    );
    Profile photo of shanebp
    shanebp
    Moderator

    @shanebp

    Please use pastebin or gist to share large code blocks.

    You could always use the filter hook to set $is_blog_public to false and then remove the filter ( add_filter, remove_filter ), as part of your custom comment handling.
    $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );

    Profile photo of wspencer
    wspencer
    Participant

    @wspencer

    I’m not using the do_action…that’s built into WordPress core in wp-includes/comment.php. The do_action is called during the wp_update_comment() function, which my custom comment editor uses. I could avoid it all together by doing a direct database insert using WP’s $wpdb class, but I prefer to use their built in functionality whenever possible.

    Profile photo of shanebp
    shanebp
    Moderator

    @shanebp

    Yes, sorry, I misread and have adjusted my comment.
    I’ve provided a suggestion re a filter hook.

    Profile photo of wspencer
    wspencer
    Participant

    @wspencer

    Thanks. But if I do that, will those comments not show up in activity streams, then?

    Profile photo of shanebp
    shanebp
    Moderator

    @shanebp

    Then in your custom handler, adapt the code in bp_blogs_record_comment or write your own using bp_activity_add

    https://codex.buddypress.org/developer/function-examples/bp_activity_add/

    Profile photo of wspencer
    wspencer
    Participant

    @wspencer

    Thanks for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘edit_comment hook in bp-blogs/bp-blogs-functions.php causing error’ is closed to new replies.