On form custom actions, I understand I have to write a bricks filter to capture the form and process how I want which isn’t an issue. My issue is can I write multiple custom actions, and then how do I declare on the form which action I want to run?
It appears all I can do is select “Custom” on the form action menu, but then nothing new appears, I guess I was expecting to declare my function name or something? Am I maybe missing something here?
Yeah, you’re missing a step. Setting custom really does nothing more than turn on the custom action hook. You’ll need to define an action somewhere in php like this.
<?php
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();
// Perform some logic here...
// Set result in case it fails
$form->set_result([
'action' => 'my_custom_action',
'type' => 'success', //or danger or info
'message' => esc_html__('Oh my custom action failed', 'bricks'),
]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
Thanks for the quick reply, and that example action I had seen. I’m trying to foreshadow a little bit, and I’m thinking I’m going to have a few forms that will need differing custom actions applied to each form separately.
Can I pass which function I’m looking to run from the form, or do I need to access the one action and then determine which form has been submitted to determine which process to follow from there?
That’s a great question. Well, it might not be the most elegant solution but you could add a hidden field to your forms called custom-action, or whatever you want, and then you can define which custom action you want to trigger on that form by assigning the value. Then in your function, you could check the fields first before doing your action:
<?php
function my_form_custom_action( $form ) {
$settings = $form->get_settings();
foreach ($settings['fields'] as $field) {
if (isset($field['label']) && $field['label'] == 'custom-action') {
if ( $field['value'] == 'action-1') {
// Perform some logic here...
}
if ( $field['value'] == 'action-2') {
// Perform some other logic here...
}
}
}
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
So the solution is to have just one custom filter and then do the processing for all possible forms/custom actions within that?
Doable but seems clumsy and not so easy to maintain. Would be much nicer to be able to call a custom filter from the form settings. Maybe a dropdown with any found filters listed to choose from?
I like your idea. I think it should be put forward as a suggestion with all the other form upgrades happening. Or if it can’t be a dropdown, at least have a field to define a custom function.
Well you are absolutely right. Form Element should get more love for sure. I’ve noticed that bricks team likes to surprise with some features even though they are not present on idea board. So I hope this will be the case as well
I’ve realized over the years with forms is to keep the form separate from the actual theme/builder. If you ever switch, your form is locked in… making migrating way more involved.
From experience… I’d suggest either a plugin or custom php form
I like your idea. On one hand it makes sense on the other not so much. We should keep stack at minimum because then its easier to update (site and plugins) and you have less vunerability places on your website. Custom PHP for forms is great, but in that case there should be better custom function creation instead of chcecking each form by ID to modify form action and webhook may be enough for all the rest . I have found one solution for webkook though it will be better to have it in place with builder.
No what I’m saying is if you get too heavy into a page builder, your migration is extra long… plus potential updates can break your setup
If you go the form plugin route, you do not have to re-setup your forms (maybe some CSS)… but you risk annoying plugin updates with potential bugs.
If you go the custom route, and ensure your form is sanitized, etc… then one less maintenance task
Note: It’s very important that each use-case is different… if just a simple contact form… I’d suggest plugin… if creating consistent opt-in forms, the custom PHP Form has been a great approach… cannot recommend it enough!