How To Use AJAX Into WordPress Skip to main content

AJAX Into WordPress

Saurabh Dhariwal

Saurabh Dhariwal

AJAX into WordPress

Ajax with WordPress

Ajax technology is being used by all types of websites and also into WordPress.
Currently, the core of WordPress uses ajax only into administration.

Note: If you are using Ajax at front-end, you need to use "wp_localize_script()" to make your JavaScript to aware of admin-ajax.php URL for JavaScript global variable "ajaxurl".

 

Action hooks for Ajax into WordPress

Here are two action hooks that allow user to create a custom handler for his/her own custom Ajax request.

  • wp_ajax_
  • wp_ajax_nopriv_

wp_ajax_ is only called at administrator side but in other hand wp_ajax_nopriv_ called front-end side.

wp_ajax_ and wp_ajax_nopriv_ hooks use like wp_ajax_$youraction or wp_ajax_nopriv_$youraction where $youraction is your Ajax request's 'action' property.

You need to create two functions one for enqueue you ajax script and another one is a callback function that handles the request.

 

Example for AJAX at administrator side:

1. Enqueuing Ajax js file.

<?php add_action('admin_enqueue_scripts', 'my_admin_ajax'); function my_admin_ajax() {   wp_enqueue_script('my-script', 'path_of_your_js_file/myscript.js',array('jquery')); }

Into myscript.js file

jQuery(document).ready(function($) {   var ajaxData = {     'action': 'my_action',     'whatever': 'Hello World'   }   jQuery.post(ajaxurl, ajaxData, function(response){     alert(response);   }); });

2. Call action and handle the request with a callback function.

add_action('wp_ajax_my_action', 'my_callback'); function my_callback() {   $whatever = $_POST['whatever'];   echo $whatever;   wp_die(); }

 

Example for AJAX at front-end side:

1. Enqueuing Ajax js file and localize script for global variable "ajaxurl".

add_action('admin_enqueue_scripts', 'my_admin_ajax'); function my_admin_ajax() {   wp_enqueue_script('my-script', 'path_of_your_js_file/myscript.js',array('jquery'));   wp_localize_script( 'my-script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); }

Into myscript.js file

jQuery(document).ready(function($) {   var ajaxData = {     'action': 'my_action',     'whatever': 'Hello World'   }   jQuery.post(myAjax.ajaxurl, ajaxData, function(response){     alert(response);   }); });


2. Call action and handle the request with a callback function.

add_action('wp_ajax_my_action', 'my_callback'); function my_callback() {   $whatever = $_POST['whatever'];   echo $whatever;   wp_die(); }

Hope you all enjoyed Ajax feature in WordPress. You can also implement similar Ajax concept for other frameworks like Drupal, Laravel, Yii, Magento, CakePHP, etc. the only change would be the syntax, Hire wordpress developer if you need help.

Feel free to share any questions, we are ready to help always :) 

Ajax is powerful JavaScript based technology which enables the website to dynamically fetch data and show data without refreshing page. This will provide more speed up website and great user experience.

 

Ajax with WordPress

Ajax technology is being used by all types of websites and also into WordPress.
Currently, the core of WordPress uses ajax only into administration.

Note: If you are using Ajax at front-end, you need to use "wp_localize_script()" to make your JavaScript to aware of admin-ajax.php URL for JavaScript global variable "ajaxurl".

 

Action hooks for Ajax into WordPress

Here are two action hooks that allow user to create a custom handler for his/her own custom Ajax request.

  • wp_ajax_
  • wp_ajax_nopriv_

wp_ajax_ is only called at administrator side but in other hand wp_ajax_nopriv_ called front-end side.

wp_ajax_ and wp_ajax_nopriv_ hooks use like wp_ajax_$youraction or wp_ajax_nopriv_$youraction where $youraction is your Ajax request's 'action' property.

You need to create two functions one for enqueue you ajax script and another one is a callback function that handles the request.

 

Example for AJAX at administrator side:

1. Enqueuing Ajax js file.

<?php

add_action('admin_enqueue_scripts', 'my_admin_ajax');

function my_admin_ajax() {
  wp_enqueue_script('my-script', 'path_of_your_js_file/myscript.js',array('jquery'));
}

Into myscript.js file

jQuery(document).ready(function($) {
  var ajaxData = {
    'action': 'my_action',
    'whatever': 'Hello World'
  }
  jQuery.post(ajaxurl, ajaxData, function(response){
    alert(response);
  });
});

2. Call action and handle the request with a callback function.

add_action('wp_ajax_my_action', 'my_callback');

function my_callback() {
  $whatever = $_POST['whatever'];
  echo $whatever;
  wp_die();
}

Example for AJAX at front-end side:

1. Enqueuing Ajax js file and localize script for global variable "ajaxurl".

add_action('admin_enqueue_scripts', 'my_admin_ajax');

function my_admin_ajax() {
  wp_enqueue_script('my-script', 'path_of_your_js_file/myscript.js',array('jquery'));
  wp_localize_script( 'my-script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}

Into myscript.js file

jQuery(document).ready(function($) {
  var ajaxData = {
    'action': 'my_action',
    'whatever': 'Hello World'
  }
  jQuery.post(myAjax.ajaxurl, ajaxData, function(response){
    alert(response);
  });
});

2. Call action and handle the request with a callback function.

add_action('wp_ajax_my_action', 'my_callback');

function my_callback() {
  $whatever = $_POST['whatever'];
  echo $whatever;
  wp_die();
}

Hope you all enjoyed Ajax feature in WordPress. You can also implement similar Ajax concept for other frameworks like Drupal, Laravel, Yii, Magento, CakePHP, etc. the only change would be the syntax, Hire WordPress Developer if you need help.

Feel free to share any questions, we are ready to help always :) 

Frequently Asked Questions

What exactly is AJAX, and why should I use it in WordPress?

AJAX, or Asynchronous JavaScript and XML, is a web technology that allows you to update parts of a page without reloading the whole thing. In WordPress, using AJAX can make your website feel faster and more dynamic.

Can you give me a real-world example of how AJAX benefits a WordPress site?

Imagine a comments section that updates without refreshing the entire page. That's AJAX in action. It provides a smoother and more interactive user experience by fetching and displaying new information without reloading the whole page.

Is AJAX difficult to implement in a WordPress site, especially for someone with coding experience?

Not at all! While it involves some coding, some plugins and tutorials simplify the process. You can add AJAX functionality to your WordPress site without being a coding expert.

What are the common use cases for using AJAX in WordPress?

AJAX is handy for loading more posts without a page refresh, submitting forms in the background, or updating content dynamically. It's a versatile tool that enhances user interaction on your site.

How do I add AJAX to my WordPress site without breaking anything?

Adding AJAX requires some coding, but step-by-step guides and plugins are available. Always back up your site before making changes, and start with small experiments to avoid unexpected issues.

Can I use AJAX with plugins and themes on my WordPress site?

Absolutely! Many plugins and themes support AJAX; you can even add AJAX functionality to your custom themes or plugins. It's a flexible feature that plays well with various WordPress components.

Does AJAX impact the performance of my WordPress site?

When used correctly, AJAX can improve performance by reducing the need for full-page reloads. However, poorly implemented AJAX calls can lead to performance issues. It's essential to follow best practices and optimize your code.

Are there security considerations when using AJAX in WordPress?

Yes, security is crucial. Continuously validate and sanitize user input, use nonces to verify requests, and implement proper permissions checks. Following WordPress coding standards helps ensure your AJAX implementation is secure.