wp_allow_comment example

if ( empty( $commentdata['comment_date_gmt'] ) ) {
        $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
    }

    if ( empty( $commentdata['comment_type'] ) ) {
        $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 ) {
            
$check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_comment );

        if ( is_wp_error( $check_comment_lengths ) ) {
            $error_code = $check_comment_lengths->get_error_code();
            return new WP_Error(
                $error_code,
                __( 'Comment field exceeds maximum length allowed.' ),
                array( 'status' => 400 )
            );
        }

        $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true );

        if ( is_wp_error( $prepared_comment['comment_approved'] ) ) {
            $error_code    = $prepared_comment['comment_approved']->get_error_code();
            $error_message = $prepared_comment['comment_approved']->get_error_message();

            if ( 'comment_duplicate' === $error_code ) {
                return new WP_Error(
                    $error_code,
                    $error_message,
                    array( 'status' => 409 )
                );
            }
Home | Imprint | This part of the site doesn't use cookies.