Create a form using PHP code
-
Hi, I want to create a form in forminator through code if a specific condition comes true. Is it possible to create a form with fields, default values etc. using backend PHP code?
-
Hi @plgupa,
You can check these methods and see if that helps:
https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-add_form
Please do let us know how that goes.
Regards,Nithin
Hi, Thanks for the reply. This is very useful.
I want to duplicate a form through code and then remove few fields (from new form) based on user input. I am doing something like this, but it did not work –
function create_form($field1, $field2, $field3, $redirect){
$main_form = Forminator_API::get_form(14);
$new_form_id = Forminator_API::add_form('Form Test 3', $main_form->$fields, $main_form->$settings);
if($field1==false){
Forminator_API::delete_form_field($new_form_id, 'text-1');
}
if($field2==false){
Forminator_API::delete_form_field($new_form_id, 'text-2');
}
if($field3==false){
Forminator_API::delete_form_field($new_form_id, 'text-3');
}
Forminator_API::update_form_setting( $form_id, 'redirect-url', $redirect);
}Please guide what am I doing wrong?
Hi, I have almost figured it out. The new form is getting created and also I am able to delete the fields. But I am not able to add redirect url to the form. It is always coming blank. I am using below code. Please help.
function create_form(){
$main_form = Forminator_API::get_form(14);
$wrappers = Forminator_API::get_form_wrappers(14);
$settings = $main_form->settings;
$settings['formName'] = 'Form Test 8';
$new_form_id = Forminator_API::add_form(
'Form Test 8',
$wrappers,
$settings
);
Forminator_API::delete_form_field($new_form_id, 'text-1');
Forminator_API::update_form_setting($new_form_id, 'submission-behaviour', 'redirect');
Forminator_API::update_form_setting($new_form_id, 'redirect-url', 'https://url');
}Hello @plgupa
You need to access the redirect rule under submission-behaviour, which is an array. I asked our developers to take a quick look at your code.
We will get back to you as soon as we have more information.
Best Regards
AminYes, please help, I still could not update the redirect url.
Hello @plgupa
We received further feedback from our development team.
You are changing the form name with this code “$settings[‘formName’] = ‘Form Test 8’;” then can simply change the “$settings[‘submission-behaviour’]” and “$settings[‘redirect-url’]” and it should be behaviour-redirect and not redirect.
Please note that since custom coding is out of scope of our support, we can’t share examples.
Best Regards
AminHi @plgupa
We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!
Kind Regards,
Kris
You must be logged in to reply to this topic.