How to use Ajax in wordpress with Jquery

In this post I want to explain that how to use Ajax in wordPress and also How its work in wordpress. There are many ways using ajax in wordpress. Ajax is a technique for display content on page without refresh page It’s a fast and smooth way for displaying content, and as a result Ajax is now used in many various ways on a website, such as submitting blog comments, posts and uploading files. You can even make your website completely Ajaxified, so that each page of your site will load asynchronously.

You may already know that using ajax in wordpress slightly different to outside of wordpress, You will need to consider somethings and also must be need to knowledge of wordpress hooks and functions for use ajax in wordpress.

In wordpress no need to include jquery file because jquery already included in wordpress, And in wordpress two things are most important for ajax first all Every Ajax request goes to admin-ajax.php and  each and every request goes to create two  action hooks.

Action hooks takes two parameters first one action and second function name, In wordpress ajax two hooks are used first wp_ajax and second wp_ajax_nopriv you can see in given example.




first hook is executed for guest user and second hook is executed for login user, this is great way for control access for users without any conditions.

Example

In this example we fetch post meta field value and increment that filed value and again update it.  Now read ajax call code which given below.

 

 

4 Comments

Add a Comment

Your email address will not be published. Required fields are marked *