Sarathlal N

Handling form using AJAX in WordPress

As a WordPress developer, regularly I need to use forms in WordPress front end & admin pages. I like to use AJAX for form submissions because it will avoid page redirection & AJAX improves user interactions.

So here is the HTML, Javascript & PHP code collection that I use normally in my plugins. Sorry, there is no detailed explanation.

HTML FORM

<form name='sample_form' method='post' id='sample_form'>
    <p>
	    <label for="user_name">User name</label>
	    <input type='text' name="user_name" />
    </p>
    <p>
	    <input type='submit' class='button' value='submit' />
	    <input type="hidden" name="action" value="prefix_your_action_name">
	    <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>	
    </p>
</form>

You have to display the form in WordPress pages using proper hooks.

JavaScript / jQuery

<script type="text/javascript" >
	(function($, window, document) {
		$('#sample_form').submit(function(e){
			e.preventDefault();
			var formData = $(this).serializeArray();
			$.ajax({
				type: 'POST',
				url : ajaxurl, // `ajaxurl` only available in admin end
				data: formData,
				beforeSend : function(){
					// action before submit
				},
				success: function(response){
					// On success
				},
				error: function(jqXHR, textStatus, errorThrown){
					// On error
				},
				complete: function(jqXHR, textStatus){
					// On complete
				}
			});
		});
	}(window.jQuery, window, document));
</script>

PHP / WordPress

// Fires authenticated Ajax actions for logged-in users.
add_action( 'wp_ajax_prefix_your_action_name', 'your_callback_function' );

// Fires non-authenticated Ajax actions for logged-out users.
add_action( 'wp_ajax_nopriv_prefix_your_action_name', 'your_callback_function' );

function your_callback_function(){
    if(!isset($_REQUEST['name_of_nonce_field']) || !(wp_verify_nonce($_REQUEST['name_of_nonce_field'], 'name_of_my_action'))){
       wp_die('error!');
    }else{
       // process form data
    }

    wp_send_json($your_result);
}

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Understanding the Singleton Pattern and Using Traits to Achieve Singleton in WordPress Plugin Development
  2. REST API Methods Explained with Best Practices for Building Clean and Secure APIs
  3. My 28-Day Plan to Master Modern WordPress Development Using AI Tools
  4. Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
  5. WordPress Transients Explained - A Developer's Guide to Site Performance

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.