plugins – How to dynamically generate multiple forms from a Template, based on user input?


I write to the community with the hope that someones can show me the correct way to solve my issue.

I’m trying to create a simple plugin that should generate multiple contact forms from a Form Template and save the data in to the WordPress Database.
The user should have the ability to choose (from the admin area) which fields to display in the form and then, through a Shortcode, the form will be displayed in the Frontend.

The Code I have is working for a unique form, but the goal would be to generate and save multiple forms to the database (not sure I should create a new table, or use an existing WP table). Then, to each form, a Shortcode would be dynamically assigned – eg: (contact_form id = “1”), etc. The generated data would then be displayed as a list table in the admin area, and the user should have the ability to add, update, and delete the generated forms.
I’ve heard about shortcode_atts() function; WP_List_Table Class, Form Tables, and Custom Post Types, but I’m not really sure how can I solve this issue.

Maybe someone can guide me in finding the proper way to deal with this issue, following the WordPress standards?

Thanks in advance!

Add menu page

<?php

/**
 * Plugin Name: Generate multiple contact forms from Form template
 */


function create_admin_pages (){
      add_menu_page( "Top Menu Page" ,"Top Menu Page", "manage_options", "top_menu_page", "top_menu_page_content_callback" );

            add_submenu_page( "top_menu_page", "General Options", "General Options", "manage_options", "top_menu_page", "top_menu_page_content_callback");
     
add_action( "admin_init", "create_settings" );
}
add_action( "admin_menu", "create_admin_pages" );
?>

Top menu page callback function

<?php

function top_menu_page_content_callback()
{
?>
    <div class="wrap">
    <form method="POST" action="options.php">
    <?php settings_fields( 'settings_group_1' );    
    do_settings_sections( 'top_menu_page' );
    submit_button();
    ?>
    </form>
    </div>
<?php
}
?>

The settings API

<?php
function create_settings(){

register_setting( "settings_group_1", "contact_form_FirstName" );
register_setting( "settings_group_1", "contact_form_LastName" );
register_setting( "settings_group_1", "contact_form_Email" );
register_setting( "settings_group_1", "contact_form_Phone" );


add_settings_section( "section_2", "Contact-Form", "section_2_callback", "top_menu_page");


add_settings_field( "contact_form_FirstName", "First Name", "contact_form_FirstName_setting_callback","top_menu_page", "section_2" );
add_settings_field( "contact_form_LastName", "Last Name", "contact_form_LastName_settings_callback","top_menu_page", "section_2" );
add_settings_field( "contact_form_Email", "Email", "contact_form_Email_setting_callback","top_menu_page", "section_2" );
add_settings_field( "contact_form_Phone", "Phone", "contact_form_Phone_settings_callback","top_menu_page", "section_2" );

}
?>

Settings Callback functions

<?php

function contact_form_FirstName_setting_callback(){

       $contact_form_FirstName = esc_attr( get_option( 'contact_form_FirstName' ) );
       echo '<input name="contact_form_FirstName" id="contact_form_FirstName" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'contact_form_FirstName' ), false ) . ' />  ';
 }

function contact_form_LastName_settings_callback(){

       $contact_form_LastName = esc_attr( get_option( 'contact_form_LastName' ) );
       echo '<input name="contact_form_LastName" id="contact_form_LastName" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'contact_form_LastName' ), false ) . ' /> ';
 }

function contact_form_Email_setting_callback(){

       $contact_form_Email = esc_attr( get_option( 'contact_form_Email' ) );
       echo '<input name="contact_form_Email" id="contact_form_Email" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'contact_form_Email' ), false ) . ' /> ';
 }

function contact_form_Phone_settings_callback(){

       $contact_form_Phone = esc_attr( get_option( 'contact_form_Phone' ) );
       echo '<input name="contact_form_Phone" id="contact_form_Phone" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'contact_form_Phone' ), false ) . ' /> ';
 }


function section_2_callback(){
      echo "Check the fields you would like to add to your form. <br> ";
}
?>

The Form Template

<?php
//Template form Section

function custom_shortcode()
{  

?>
<form action="" method="POST">
<?php
    
    $contact_form_FirstName = get_option('contact_form_FirstName');
    if ($contact_form_FirstName == '1')
    {
        echo '<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" required/><br>';
    }

  
    $contact_form_LastName = get_option('contact_form_LastName');
    if ($contact_form_LastName == '1')
    {
        echo '<label for="last_name">Last Name</label><input  id="last_name" maxlength="40" name="first_name" size="20" type="text" required/><br>';
    }

   
    $contact_form_Email = get_option('contact_form_Email');
    if ($contact_form_Email == '1')
    {
        echo '<label for="email">Email</label><input  id="email" maxlength="80" name="email" size="20" type="email" required/><br>';
    }
 
    $contact_form_Phone = get_option('contact_form_Phone');
    if ($contact_form_Phone == '1')
    {
        echo '<label for="phone">Phone</label><input  id="phone" maxlength="40" name="phone" size="20" type="text" required/><br>';
    }
?>
<input type="submit" name="submit">
</form>
<?php
}

add_shortcode('contact_form', 'custom_shortcode');
?>