bp_activity_new_comment( array|string $args = '' )

Add an activity comment.

Description

Parameters

$args

(Optional) An array of arguments.

  • 'id'
    (int) Optional. Pass an ID to update an existing comment.
  • 'content'
    (string) The content of the comment.
  • 'user_id'
    (int) Optional. The ID of the user making the comment. Defaults to the ID of the logged-in user.
  • 'activity_id'
    (int) The ID of the "root" activity item, ie the oldest ancestor of the comment.
  • 'parent_id'
    (int) Optional. The ID of the parent activity item, ie the item to which the comment is an immediate reply. If not provided, this value defaults to the $activity_id.
  • 'primary_link'
    (string) Optional. the primary link for the comment. Defaults to an empty string.
  • 'skip_notification'
    (bool) Optional. false to send a comment notification, false otherwise. Defaults to false.
  • 'error_type'
    (string) Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.

Default value: ''

Return

(WP_Error|bool|int) The ID of the comment on success, otherwise false.

Source

File: bp-activity/bp-activity-functions.php

2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
function bp_activity_new_comment( $args = '' ) {
    $bp = buddypress();
 
    $r = wp_parse_args( $args, array(
        'id'                => false,
        'content'           => false,
        'user_id'           => bp_loggedin_user_id(),
        'activity_id'       => false, // ID of the root activity item.
        'parent_id'         => false, // ID of a parent comment (optional).
        'primary_link'      => '',
        'skip_notification' => false,
        'error_type'        => 'bool'
    ) );
 
    // Error type is boolean; need to initialize some variables for backpat.
    if ( 'bool' === $r['error_type'] ) {
        if ( empty( $bp->activity->errors ) ) {
            $bp->activity->errors = array();
        }
    }
 
    // Default error message.
    $feedback = __( 'There was an error posting your reply. Please try again.', 'buddyboss' );
 
    // Bail if missing necessary data.
    if ( empty( $r['content'] ) || empty( $r['user_id'] ) || empty( $r['activity_id'] ) ) {
        $error = new WP_Error( 'missing_data', $feedback );
 
        if ( 'wp_error' === $r['error_type'] ) {
            return $error;
 
        // Backpat.
        } else {
            $bp->activity->errors['new_comment'] = $error;
            return false;
        }
    }
 
    // Maybe set current activity ID as the parent.
    if ( empty( $r['parent_id'] ) ) {
        $r['parent_id'] = $r['activity_id'];
    }
 
    $activity_id = $r['activity_id'];
 
    // Get the parent activity.
    $activity  = new BP_Activity_Activity( $activity_id );
 
    // Bail if the parent activity does not exist.
    if ( empty( $activity->date_recorded ) ) {
        $error = new WP_Error( 'missing_activity', __( 'The item you were replying to no longer exists.', 'buddyboss' ) );
 
        if ( 'wp_error' === $r['error_type'] ) {
            return $error;
 
        // Backpat.
        } else {
            $bp->activity->errors['new_comment'] = $error;
            return false;
        }
 
    }
 
    // Check to see if the parent activity is hidden, and if so, hide this comment publicly.
    $is_hidden = $activity->hide_sitewide ? 1 : 0;
 
    /**
     * Filters the content of a new comment.
     *
     * @since BuddyPress 1.2.0
     * @since BuddyPress 3.0.0 Added $context parameter to disambiguate from bp_get_activity_comment_content().
     *
     * @param string $r       Content for the newly posted comment.
     * @param string $context This filter's context ("new").
     */
    $comment_content = apply_filters( 'bp_activity_comment_content', $r['content'], 'new' );
 
    // Insert the activity comment.
    $comment_id = bp_activity_add( array(
        'id'                => $r['id'],
        'content'           => $comment_content,
        'component'         => buddypress()->activity->id,
        'type'              => 'activity_comment',
        'primary_link'      => $r['primary_link'],
        'user_id'           => $r['user_id'],
        'item_id'           => $activity_id,
        'secondary_item_id' => $r['parent_id'],
        'hide_sitewide'     => $is_hidden,
        'error_type'        => $r['error_type']
    ) );
 
    // Bail on failure.
    if ( false === $comment_id || is_wp_error( $comment_id ) ) {
        return $comment_id;
    }
 
    // Comment caches are stored only with the top-level item.
    wp_cache_delete( $activity_id, 'bp_activity_comments' );
 
    // Walk the tree to clear caches for all parent items.
    $clear_id = $r['parent_id'];
    while ( $clear_id != $activity_id ) {
        $clear_object = new BP_Activity_Activity( $clear_id );
        wp_cache_delete( $clear_id, 'bp_activity' );
        $clear_id = intval( $clear_object->secondary_item_id );
    }
    wp_cache_delete( $activity_id, 'bp_activity' );
 
    if ( empty( $r[ 'skip_notification' ] ) ) {
        /**
         * Fires near the end of an activity comment posting, before the returning of the comment ID.
         * Sends a notification to the user @see bp_activity_new_comment_notification_helper().
         *
         * @since BuddyPress 1.2.0
         *
         * @param int                  $comment_id ID of the newly posted activity comment.
         * @param array                $r          Array of parsed comment arguments.
         * @param BP_Activity_Activity $activity   Activity item being commented on.
         */
        do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
    } else {
        /**
         * Fires near the end of an activity comment posting, before the returning of the comment ID.
         * without sending a notification to the user
         *
         * @since BuddyPress 2.5.0
         *
         * @param int                  $comment_id ID of the newly posted activity comment.
         * @param array                $r          Array of parsed comment arguments.
         * @param BP_Activity_Activity $activity   Activity item being commented on.
         */
        do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
    }
 
    if ( empty( $comment_id ) ) {
        $error = new WP_Error( 'comment_failed', $feedback );
 
        if ( 'wp_error' === $r['error_type'] ) {
            return $error;
 
        // Backpat.
        } else {
            $bp->activity->errors['new_comment'] = $error;
        }
    }
 
    return $comment_id;
}

Changelog

Changelog
Version Description
BuddyPress 2.6.0 Added 'error_type' parameter to $args. BuddyPress 2.6.0 Added 'error_type' parameter to $args.
BuddyPress 1.2.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.