Create a form programmatically

By: Filip Pejakovic | Asked: 12/16/2023
ForumsCategory: Code SnippetsCreate a form programmatically
Filip Pejakovic asked 4 months ago

Hello, I want to create a form programmatically after a user is registered through the registration form.
I have achieved duplication but the conditionals and fields keep the old field values from the form i duplicated instead of updating the interconnections between the fields. This is my code:

function create_form_on_user_register( $entry_id, $form_id ) {
    if ( $form_id == 4 ) {
        $email = $_POST['item_meta'][109];
        $user = get_user_by('email', $email);
        $user_id = $user->ID;

        if (in_array('contributor', $user->roles)) {
            if (class_exists('FrmForm')) {
                $employee_template_form_id = 11;
                $template = false;
                $copy_keys = true;
                $company_name = $_POST['item_meta'][115];
                $employee_survey_name = $company_name . ' Employee Form';
       
                $employee_form_id = FrmForm::duplicate($employee_template_form_id, $template, $copy_keys);
                echo $form_file_path;

                if ($employee_form_id) {
                    $update_result = FrmForm::update($employee_form_id, array('name' => $employee_survey_name));
                }
               
                // Additional data you want to store
                update_user_meta($user_id, 'employee_survey_id', $employee_form_id);
       
                // Create a new page for Employer survey
                $employee_survey_page = wp_insert_post(array(
                    'post_title'    => $company_name . ' Employee Survey',
                    'post_content'  => '[formidable id=' . $employee_form_id . ']',
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                ));
            }
        }
    }
}
1 Answers
Rob LeVineRob LeVine answered 4 months ago
It seems like this is a bug you should report to Formidable (aka S11) Support. I didn't by any means do an exhaustive search of the code, however, I don't see any place where the code is duplicating the options and then replacing the old ids with new ids in the conditional logic section of the options. Maybe it's an oversight. Maybe I'm wrong :).
Filip Pejakovic replied 4 months ago

Shouldn't be a bug but thanks for the idea. There is surely a way to duplicate options, I just thought that the duplicate method handles all that.

Rob LeVineRob LeVine replied 4 months ago

I'm not sure what you mean by "shouldn't be a bug". At the very least it should be a feature request, though it seems to me like an oversight. It's a fair expectation that the form duplication process should duplicate the form's fields as thoroughly as possible and part of that is duplicating the conditional logic. If you mean there should be a way to do it yourself; grab the old field options, figure out the new field IDs that should be substituted in there, and write out the new field options.

Filip Pejakovic replied 4 months ago

What i meant is i want to do it with code myself, the Duplicate Form works fine on it's own. However i have found what the problem is, the hooks aren't all being loaded and that's why it wasn't working.

Filip Pejakovic replied 4 months ago

Actually from the backend when i try to register a user it works, the hooks just aren't loaded on the frontend when i submit the form, otherwise it's fine.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right