That’s How You Use AJAX With PHP In Your WordPress Website!

When managing WordPress website, you have to know about different languages that help it function. Even if you don’t have to know about coding for creating website; you must know the languages. The one that helps these languages in creation is AJAX! Before we go to use AJAX with PHP, let’s know more about AJAX first. It is one of the technics of web developers that helps to develop interactive applications. With these applications your website could work smoother and faster. This will allow users to update content in website without reloading it. For working with AJAX you need to know other languages as well.

AJAX basically stands for ‘Asynchronous JavaScript and XML’. This is used to create interactive and entertaining applications. The most popular example of this is Google Maps. Along with it the autocomplete feature in Google search, comments and likes on social media posts etc.

Some Things You Should Know About AJAX

AJAX works with all the web languages. Putting together with JavaScript, HTML, CSS and XML it can create faster web experience. There’s another pair that works with web development along with these languages. AJAX with PHP is the other server side language that works with web development.

It uses JavaScript for the content and CSS for presentation and document object model. It also uses HTML for further process. Usually web applications or web pages exchange information with the server synchronously. Whereas in the web application created using AJAX, when you click a button or fill a form the JavaScript create XMLHTTP request. It send this request to the server as well in XML format.

The server creates a server-side response and sends it to browser. Then JavaScript processes this response. The content on the screen gets updated. This doesn’t involve reloading or refreshing of the website. This way the users won’t be aware of sending information to server.

To work with AJAX you need to have certain skills. As mentioned earlier, to work with AJAX you have to know about languages like JavaScript, HTML and CSS. You need to be proficient in the server-side language like PHP and others. And lastly, you need to know some basics about XML and JSON.

AJAX in WordPress

In WordPress AJAX is used at a backend. Due to this when you make any changes into posts, moderate comments or make changes into categories the update will be instantly made. The AJAX is mostly used with JQuery in WordPress. WordPress uses AJAX in definite process!

After making the request it goes through ‘admin-ajax.php’ file in wp-admin folder. The requests would need a piece of data which is called ‘action’. It can be processed by using ‘get’ or ‘post’. This will prompt the code in ‘admin-ajax.php’ file to create hooks. The hooks are ‘wp_ajax_my_action’ and ‘wp_ajax_nopriv_my_action’. The phrase ‘my_action’ shows the value of variable ‘action’ of ‘get’ and ‘post’ method.

The first hook is for the actions taken by logged in users. Whereas the second hook is for the logged out users. These hooked actions are for graceful degradation. It will ensure that if the JavaScript is disabled in the browsers the code will still work.

A WordPress AJAX Plugin

You can create a WordPress AJAX plugin by following these steps. If done carefully, it wouldn’t take more than few minutes. To start the process first you need to take an empty WordPress plugin created and activated in your website.

Step 1:

Then get a good name for this plugin. You can check if the plugin name is already taken by checking the plugin repositories.

Step 2:

Now you need to create a PHP file using this plugin name. The plugin should not share the PHP file. Your plugin will require users to put the PHP file in WordPress plugin directory. That’s why the plugin shouldn’t have common name or PHP file.

Step 3:

The plugin must have at least one PHP file along with JavaScript, CSS languages and an image file. If you find multiple files then put a unique name for directory and for main PHP file. Put all the plugin files in directory. Request the users to upload whole directory in ‘wp-content/plugin/’ directory.

The WordPress can change the standard plugin directory. That’s why you should use ‘plugin_dir_path()’ and ‘plugin_url()dor’ path and URLs in PHP code.

We have successfully created WordPress AJAX plugin with PHP. Now we need to find the ‘single_php’ post template in the theme name. For this you need to go to the root directory of current website. This file should be open for edit.

For users to like the post there has to be a link. If you have enabled JavaScript then you can use JavaScript files. If not then you can use the link directly. For this you need to put codes in the ‘single_php’ file. You can add the code into any of the template’s parts.

Now we have created the required link successfully. This link will forward you to the ‘admin_ajax.php’ script. They can’t run any action in it yet. For creating the function in plugin file and to add it in hooks created on WordPress you need to insert a code.

By putting this code, when the user will click on ‘like this post’ the number of likes will be updated.

Adding Javascript Support

If you have a support from JavaScript, the whole thing would be a lot easier. To use AJAX with PHP you need to enqueue the jQuery library. You need to do it along with custom JavaScript file of the plugin. For this you need to write a certain code.

After this you need to create the JavaScript file ‘liker_scripts.js’. This file is supposed to be uploaded in the plugin folder of root directory of your website. To create this file you will have to write a code as well.

These are some ways to use AJAX with PHP in WordPress!

Our best WordPress theme bundle is packed with all professional WordPress themes. All the themes have advanced features for you to create the best website for business, blog or services.

Back to blog