How to Create a Form in WordPress Without a Plugin

WordPress is a widely recognized Content Management System (CMS) renowned for its ease of use in website development and management. Among its many features, WordPress offers the capability to create forms, crucial for gathering information from website visitors. While plugins like Forms Lite exist to simplify form creation, you might prefer to create forms in WordPress without relying on additional plugins.

How to Make a WordPress Form Without Using a Plugin

Whether you're a beginner or an experienced WordPress user, this guide aims to empower you to take charge of your website's forms and customize them to suit your specific requirements. So, let's delve into learning how to create a form in WordPress without using a plugin!

Would you like to design a form in WordPress without relying on plugins? Forms are crucial components of a website, and there are numerous benefits to creating them without the use of plugins.

Why is it Possible to Create a Form without a Plugin?

WordPress is a flexible platform that empowers developers and website owners to customize and expand its functionality. The core WordPress software offers features and tools that facilitate the creation of forms without the necessity of plugins.

By utilizing HTML, CSS, and PHP, you have the capability to construct a form in WordPress from the ground up and manage form submissions using WordPress's inherent capabilities. This approach grants you increased control over the form's design, functionality, and data management.

Why Create a Form in WordPress Without Plugins

There exist numerous WordPress form plugins available for creating forms. However, crafting a form in WordPress without utilizing plugins can prove advantageous for several reasons:

1. Simplicity and Lightweight: By eschewing plugins, you evade the additional overhead associated with their installation and upkeep. A straightforward form built with custom code can be more streamlined and efficient compared to one generated through a plugin.

2. Customization: Developing a form from scratch grants you complete authority over its design and functionality. You can tailor the form to align precisely with your website's distinct requirements and personalize it accordingly.

3. Learning Experience: Constructing a form without plugins can serve as a valuable learning opportunity for developers or users seeking to enhance their coding skills. This process allows for a deeper comprehension of the underlying mechanisms involved in form handling and data processing.

4. Reduced Dependency: By abstaining from plugins, you diminish your website's reliance on external code. This practice can be beneficial in mitigating potential security vulnerabilities or conflicts arising from the utilization of multiple plugins.

5. Performance: Custom-coded forms can deliver superior performance and speed for your website since they are lightweight in comparison to plugins. These bespoke forms incorporate only the essential features, eliminating the excess functionalities provided by plugins that may remain unused.

6. Flexibility: Custom forms offer heightened flexibility for seamless integration with other website components or custom post types. You can expand the form's functionality in accordance with your specific requirements.

While most plugins offer similar options via shortcodes, custom-coded forms afford the flexibility to position them precisely on the website as desired.

How to create a WordPress form without using any plugin

1) Create a Page for the Form

Navigate to "Pages" and select "Add New" from your WordPress dashboard.

This will open the page editor, allowing you to input the page title and any additional descriptions necessary apart from the actual contact form. However, if a form is all you require on this page, we can proceed to create the form directly.


2) Add HTML Code for Form in the Editor

<form method="post">
  <label for="name">Name:</label>
<input type="text" name="name" required="">

<label for="email">Email:</label>
<input type="email" name="email" required="">

<label for="message">Message:</label>
<textarea name="message" required=""></textarea>

<input type="submit" value="Submit">
</form>
This code enables you to generate a basic contact form in WordPress. However, you can readily adjust it to craft the form of your choice on your WordPress site.

Once you've incorporated all the required form fields into the code, click on "Publish" or "Update" to preserve the alterations on the contact page.


If you preview the page, you'll be able to view the form you've just created. However, to make the form operational and extract data from it, you'll need to manage the form submissions. This entails creating a database to store the form submissions afterward.


3) Create a Database Table for Form Submission

To establish a database for form submissions, you can utilize a database management tool such as phpMyAdmin. You can access phpMyAdmin by logging into your cPanel. If you lack the required credentials, we suggest reaching out to your hosting service provider or the site owner.

Once logged in, navigate to the Database section and select phpMyAdmin. This action will launch the phpMyAdmin dashboard, where you'll find all the databases associated with your website.


Expand the table of your website database and scroll to the bottom of the page. Here, you'll find an option to create a new database table. Enter the table name as "wp_contact_form_submission" and click on "Go" to initiate the creation of the new table.


Next, you'll need to input the data fields for the form fields into the table's columns along with their respective types. Based on the form we've just created, add the following column names and types:

  • name: TEXT
  • email: TEXT
  • message: TEXT
  • submission_time: DATETIME

Once you've added these columns and types, click on "Save" to preserve the changes in the database table.

We've named the database table in accordance with the contact form we've created. Therefore, feel free to modify the table's name to align with the form you initially created. Likewise, if you've utilized different form fields on your form, you can add the columns accordingly to reflect those changes.

4) Add Code to Handle Form Submissions

After creating the database, you'll need to insert a set of codes to manage the form submissions in the theme functions file of your website.

However, we strongly advise backing up your website before proceeding, as we'll be editing some of the website's core files. Any unintended modifications could potentially cause further issues on your website. You can refer to our comprehensive guide on how to backup a WordPress website if you require assistance.

4.1) Access the Theme Functions File

Navigate to "Appearance" and select "Theme File Editor" from your WordPress dashboard to access the theme functions file. Here, you'll find all the core theme files of your website. Then, choose the "Theme Functions (functions.php)" file located on the right side of your screen, where we will incorporate the code.

4.2) Add the Code to the Theme File Editor

You can utilize the following code to manage the form submission on your website. Simply add the following code at the end of the editor:

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $name = sanitize_text_field($_POST["name"]);
  $email = sanitize_email($_POST["email"]);
  $message = sanitize_textarea_field($_POST["message"]);

  // Add code to save the form data to the database
  global $wpdb;
  $table_name = $wpdb->prefix . 'contact_form_submissions';
  $data = array(
    'name' => $name,
    'email' => $email,
    'message' => $message,
    'submission_time' => current_time('mysql')
  );
  $insert_result = $wpdb->insert($table_name, $data);

  if ($insert_result === false) {
    $response = array(
      'success' => false,
      'message' => 'Error saving the form data.',
    );
  } else {
    $response = array(
      'success' => true,
      'message' => 'Form data saved successfully.'
    );
  }

  // Return the JSON response
  header('Content-Type: application/json');
  echo json_encode($response);
  exit;
}

This code snippet will handle the form submission on your website. Be sure to add it at the end of the theme functions file editor.

This code will store the entered form data from the form to the form submission database table we just created. Additionally, we've included a JSON response to confirm that you've correctly added the database table names and fields on your form when editing the code. You can remove this JSON response after the form data has been successfully stored in the database.

Simply click on "Update File" after you have made all the necessary changes to the code.

5) Display Form Submissions on your Dashboard

After the form data is stored in the database, you can create a dashboard menu to view the form submissions. To do so, we'll include a code to display it as well. 

You can add the following code to the theme functions file (functions.php), just like in the previous step.

function display_contact_form_submissions_page() {
  global $wpdb;
  $table_name = $wpdb->prefix . 'contact_form_submissions';
  $form_data = $wpdb->get_results("SELECT * FROM $table_name WHERE name <> '' AND email <> '' AND message <> '' ORDER BY submission_time DESC", ARRAY_A);

  ?>
  <div class="wrap">
    <h1>Contact Form Submissions</h1>
    <table class="wp-list-table widefat fixed striped">
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
          <th>Message</th>
          <th>Submission Time</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($form_data as $data) : ?>
          <tr>
            <td><?php echo esc_html($data['name']); ?></td>
            <td><?php echo esc_html($data['email']); ?></td>
            <td><?php echo esc_html($data['message']); ?></td>
            <td><?php echo esc_html($data['submission_time']); ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php }

function register_contact_form_submissions_page() {
  add_menu_page(
    'Contact Form Submissions',
    'Form Submissions',
    'manage_options',
    'contact_form_submissions',
    'display_contact_form_submissions_page',
    'dashicons-feedback'
  );
}
add_action('admin_menu', 'register_contact_form_submissions_page');

Once again, click on "Update File" after you paste and edit the code according to the form and database table you created.

Now, if you navigate to your WordPress dashboard, you will find a "Contact Form Submissions" menu. This menu will display all the form submissions from the form you initially created.


Debug Options to Display Form Submissions

If you cannot see the form data, we recommend adding code for debug output and SQL query in the code. You can include the codes `var_dump($form_data);` and `var_dump($wpdb->last_query);` respectively after the query.

So the code may look something like this:
function display_contact_form_submissions_page() {
  global $wpdb;
  $table_name = $wpdb->prefix . 'wp_contact_form_submissions';
  $form_data = $wpdb->get_results("SELECT * FROM $table_name ORDER BY submission_time DESC", ARRAY_A);

  var_dump($form_data); // Debug output
  var_dump($wpdb->last_query); // Debug SQL query

  ?>
  <!-- Rest of the code... -->
  <?php
}

Based on the debug report, you can further edit the codes to ensure the form data are displayed correctly. Additionally, you can review the points in the next section to confirm that there haven't been any errors in the code you used.

Essential Considerations for Creating an Efficient Custom Form

Here are some factors to consider while editing the code to create your custom form:

1. Check the database: Ensure that the form data is saved correctly in the correct table. For this tutorial, it should be the 'wp_contact_form_submissions' table.

2. Check for errors: Review your PHP error logs or enable error reporting to identify any errors related to form submission or data display. Errors may provide insights into what might be going wrong.

3. Verify the form submission process: Confirm that the form data is being submitted correctly and that the PHP code for saving the data executes without errors. Check if the form data is passed correctly to the PHP code upon form submission.

4. Verify table name: Double-check that the table name used in the 'display_contact_form_submissions_page()' function matches the actual table name in the database. Ensure it is 'wp_contact_form_submissions' or adjust accordingly.

5. Clear Cache: If you're using caching plugins or server-side caching, clear the cache to ensure you're viewing the latest data.

6. Permissions: Ensure that the user role you're logged in with has the 'manage_options' capability to access the custom admin page. This capability allows administrators to access the page by default.

The provided code serves as a framework for creating a contact form in WordPress. However, you'll need to customize the code if you wish to create a different form or an alternative form with varying data fields. The above points can assist you in creating your custom form on WordPress effectively.

Conclusion

This is how you can create a form in WordPress without using plugins. Of course, you can also create forms using plugins, but crafting a custom-coded form offers numerous advantages if you have a basic understanding of programming.

With the basic set of codes included in this tutorial, you can easily create a form. To summarize, here are the fundamental steps to build an effective form:

1. Add the form code to the page editor.
2. Create a database for form submissions.
3. Incorporate necessary codes to handle form submissions and display them.

We hope you now feel confident in creating a form on your website using the codes provided in this tutorial. You can customize them to include additional form fields based on your requirements. If you encounter any issues, we've included debug options and factors to consider while building custom code on WordPress.

So, have you ever attempted to create a form in WordPress without using a plugin?

FAQs (Frequently Asked Questions):

Can I create form in WordPress without plugin?

The core WordPress software provides features and tools that facilitate the creation of forms without the need for plugins. By utilizing HTML, CSS, and PHP, you can construct a form in WordPress from the ground up and handle form submissions using WordPress's native capabilities.

How do you validate a form in WordPress without a plugin?

A WordPress form can undergo validation using HTML and CSS without the reliance on plugins or custom post types. This involves adding the necessary attributes to the form elements and utilizing JavaScript to verify if the form is correctly filled out before submission.

How to create a file upload form in WordPress without plugin?

To upload files to WordPress without plugins, the easiest method is by using the drag-and-drop functionality within the WordPress block editor. This feature is available exclusively in the block editor, not the classic editor. Here's how to do it:

1. Open the post or page where you wish to add the file in the block editor.
2. Simply drag and drop the desired file directly into the editor area.

This straightforward process allows you to upload files seamlessly without the need for additional plugins.