wp_insert_comment example

$commentdata['comment_type'] = 'comment';
    }

    $commentdata = wp_filter_comment( $commentdata );

    $commentdata['comment_approved'] = wp_allow_comment( $commentdata$wp_error );

    if ( is_wp_error( $commentdata['comment_approved'] ) ) {
        return $commentdata['comment_approved'];
    }

    $comment_id = wp_insert_comment( $commentdata );

    if ( ! $comment_id ) {
        $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );

        foreach ( $fields as $field ) {
            if ( isset( $commentdata[ $field ] ) ) {
                $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field$commentdata[ $field ] );
            }
        }

        $commentdata = wp_filter_comment( $commentdata );

        

        $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment$request );
        if ( is_wp_error( $prepared_comment ) ) {
            return $prepared_comment;
        }

        $comment_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_comment ) ) );

        if ( ! $comment_id ) {
            return new WP_Error(
                'rest_comment_failed_create',
                __( 'Creating comment failed.' ),
                array( 'status' => 500 )
            );
        }

        if ( isset( $request['status'] ) ) {
            $this->handle_status_param( $request['status']$comment_id );
        }
Home | Imprint | This part of the site doesn't use cookies.