Wondering if you can use BF forms to create comments on CPTs conditionally (user provides email)?
Not yet, but itâs in the roadmap ![]()
I needed the same functionality and came up with this code (using info from bricks academy)
function my_form_custom_action( $form ) {
$fields = $form->get_fields();
// $formId = $fields['formId'];
// $postId = $fields['postId'];
// $settings = $form->get_settings();
// $files = $form->get_uploaded_files();
$comment_data = [
'comment_post_ID' => $fields['form-field-postid'],
'comment_author' => $fields['form-field-name'],
'comment_author_email' => $fields['form-field-email'],
'comment_author_url' => 'http://',
'comment_content' => $fields['form-field-comment'],
'comment_type' => 'comment',
'comment_parent' => 0,
'comment_date' => null,
'comment_approved' => 1,
];
try {
wp_insert_comment( wp_slash($comment_data) );
// Success Message
$form->set_result([
'action' => 'my_custom_action',
'type' => 'success',
'message' => esc_html__('New comment', 'bricks'),
]);
} catch (\Exception $e) {
// Error message
$form->set_result([
'action' => 'my_custom_action',
'type' => 'error',
'message' => esc_html__('Something went wrong' . $e->getMessage(), 'bricks'),
]);
}
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
Just choose âCustomâ in form actions tab. Iâm sure this will work with the native bricks form as well
Create AND update comments, with some comment management features like time allowed to edit, edit reason, perhaps revisions.
Did you already develop this ? Or is it still on the roadmap ?
I cant see any action related to this in Pro Forms.
Just bumping this post. Bump.
i need it too
Hi everyone, thanks for the patience and for bumping this.
Quick honest status: there is still no dedicated âCreate Commentâ or âUpdate Commentâ action in Pro Forms. The current action list covers posts (Create / Update / Delete Post, Post Meta), users, options, storage, payments, email lists, webhook and a few more, but nothing that inserts a WordPress comment natively. So it has not been built yet, it is still on the wishlist.
The good news: you do not need to wait for it. The approach @Illarion posted above still works, you just hook into the Custom action.
- In your form, add the action âCustomâ in the Actions tab.
- Drop a function into your code snippets / child theme.
Pro Forms fires two hooks from the Custom action:
bricksforge/pro_forms/custom_action(Bricksforge Pro Forms)bricks/form/custom_action(kept for backward compatibility with native Bricks forms)
So Illarionâs snippet keeps working. A version against the Pro Forms hook:
add_action( 'bricksforge/pro_forms/custom_action', function( $form ) {
$fields = $form->get_fields();
$comment_data = [
'comment_post_ID' => $fields['form-field-postid'],
'comment_author' => $fields['form-field-name'],
'comment_author_email' => $fields['form-field-email'],
'comment_content' => $fields['form-field-comment'],
'comment_type' => 'comment',
'comment_parent' => 0,
'comment_approved' => 1,
];
$comment_id = wp_insert_comment( wp_slash( $comment_data ) );
if ( ! $comment_id ) {
$form->set_result([
'action' => 'create_comment',
'type' => 'error',
'message' => esc_html__( 'Comment could not be created', 'bricks' ),
]);
}
}, 10, 1 );
One important note on the field keys: replace form-field-postid, form-field-name, etc. with your real field IDs. The fastest way to confirm them is a quick error_log( print_r( $form->get_fields(), true ) ); inside the function, then check your debug log after a submission.
Updating comments and the management features @Jacobsen mentioned (edit window, edit reason, revisions) are not part of this either. You can extend the same snippet with wp_update_comment() and your own logic for now.
I have noted the demand here for a native comment action. Hope the snippet unblocks you in the meantime.
Best,
Daniele