Today we will see how we can use ajax in wordpress. Generally everyone knows how to implement ajax using jquery or javascript and PHP scripting language.
Follow the steps below in order to use wordpress ajax.

1. Put ajax url in head:
In head section of your html put the following code


<script type="text/javascript">// <![CDATA[
 var st_ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';

// ]]></script>

this is the url where ajax request will be sent.

2.Make ajax request.


jQuery('a').on('click',function(e){

 e.preventDefault();

var sample_data = 'xyz'; // this is any data to sent in ajax request

 var ajaxdata = {
 action: 'your_action', // this is the action. pay attention to this we will be using this action in next step

data : sample_data
 };

 jQuery.post( st_ajaxurl, ajaxdata, function(res){

// here in variable res you will get response. So you can do anything over here.

});
 });

3. Hook to wordpress ajax action

WordPress provides some special hooks for handling ajax. We can use it in following manner.


function st_ajax_fn(){

// do the stuff here whatever you wish to
 die(1);
}
add_action('wp_ajax_your_action', 'st_ajax_fn'); // remember "your_action" is the action defined in step 2
add_action('wp_ajax_nopriv_your_action', 'st_ajax_fn'); // here too same for action

Whatever thing will be echo from this function will get collected in res variable of step 2.

In this way we can use wordpress ajax, it is too simple and robust at the same time.

If you have any queries or suggestion don’t forget to put your cursor over comment form. 🙂

Using ajax in wordpress

3 thoughts on “Using ajax in wordpress

  • December 23, 2014 at 6:57 pm
    Permalink

    Hi I read the tutorial it is nice but I have one doubt,when I hover on the html element how should I get the text or value of that element in wordpress.For eg.
    Below is code:
    Envato
    If i hover on the li tag on our page then I should get the value of the li tag as Envato in wordpress.
    Please will you guide me or write a tutorial on that.Thanks

    Reply
    • December 23, 2014 at 7:06 pm
      Permalink

      Hi Jayesh, I believe that you were trying to paste some HTML markup in comment section. Comment would not allow you to use Markup, so better way is to use pastebin.com and put your code there. After that we can resolve your issue in a better way. 🙂

      Reply
  • February 12, 2016 at 7:08 pm
    Permalink

    I’m trying to put some claissc album covers on t-shirts and some other merch to sell. I’m not sure how I would go about licensing the images I’m thinking maybe the label owns the image rights? It would seem like a pain in the neck to try to contact each label and actually get through to someone who would be responsible for such things. I’m wondering if their is an easier more efficient and standard way to go about this. Thanks for any help you may be able to provide.

    Reply

Leave a Reply

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