How to Create an AJAX Form in WordPress Without Using Plugins

code

WordPress provides a convenient way to process AJAX requests, allowing you to update data on your website without reloading the page. This makes interacting with your website smoother and more modern.

Although the official WordPress directory offers many decent plugins for creating forms — with validation, anti-spam, Captcha integration, and other useful features — they have their drawbacks. For example, they can overload the site with unnecessary scripts and styles, even if the form is only displayed on one page. Also, not every plugin will be suitable for specific tasks. In such cases, it is easier and more effective to implement the form yourself.

  1. Creating an HTML form

The first step is to create a simple form with fields for name, phone number, and message. It can be placed directly in the template or added via a shortcode or hook.

  1. Connecting scripts

For the form to work, you will need a JavaScript file responsible for sending data via AJAX. This file is connected via functions.php and is also localized to pass the request handler address and security code (nonce).

It is important to note that the nonce is valid for a limited time (usually 24 hours), so if you are using a cache, make sure it is cleared regularly.

  1. Writing JavaScript

The script is responsible for collecting form data and sending it to the server. It handles possible errors, notifies the user of successful submission, and resets the form if necessary. It also displays visual prompts if fields are not filled in.

  1. Request processing on the server

A handler is created on the server side that:

  • Checks the security of the request using a nonce;
  • Performs a basic check of the filled fields;
  • Filters out suspicious submissions (e.g., spam bots);
  • If there are errors, sends them back to the user;
  • If successful, generates an email and sends it to the specified address.

The processing logic can be placed directly in functions.php, but it is better to put it in a separate file and connect it via require_once.

Conclusion

This AJAX form in WordPress works quickly, does not overload the site with unnecessary scripts, and can be adapted to any task. At the same time, it provides basic protection and convenient user interaction. This is an excellent solution if you need a custom form without using heavy plugins.