How to use AJAX in Wordpress

Source: Internet
Author: User
Tags json rand


1. Which two AJAX methods are available?

1.1. Official method
 

The main idea is:

Add wp_enqueue_script and wp_localize_script in functions to add the AJAX request address and call script. For example:

$ AJAX = array
(
'URL' => admin_url ('admin-ajax. Php '),
);
$ AJAXURL = get_bloginfo ('Template _ url'). '/public/js/Test_ajax.js ';
Wp_enqueue_script ('My-ajax-request', $ AJAXURL, array ('jquery '));
Wp_localize_script ('My-ajax-request', 'Ajax ', $ AJAX );
 
Function _ aaaa ()
{
$ Id = $ _ POST ['id'];
$ Json ['info'] = 'Rand = '. rand (0, $ id );
Header ("Content-Type: application/json ");
Echo json_encode ($ json );
Exit;
}
Add_action ('WP _ ajax_aaaa ',' _ aaaa '); // ajax for logged in users
Add_action ('WP _ ajax_nopriv_aaaa ',' _ aaaa '); // ajax for not logged in users
Of course, you can also do this:

$ AJAX = array
(
'URL' => admin_url ('admin-ajax. Php '),
);
$ AJAXURL = get_bloginfo ('Template _ url'). '/public/js/Test_ajax.js ';
Wp_enqueue_script ('My-ajax-request', $ AJAXURL, array ('jquery '));
Wp_localize_script ('My-ajax-request', 'Ajax ', $ AJAX );
 
Function _ aaaa ()
{
$ Id = $ _ POST ['id'];
$ Respond = 'Rand = '. rand (0, $ id );
Echo $ respond;
Die ();
}
Add_action ('WP _ ajax_aaaa ',' _ aaaa '); // ajax for logged in users
Add_action ('WP _ ajax_nopriv_aaaa ',' _ aaaa '); // ajax for not logged in users
1.2. Folk methods

This method is used to call related functions in functions by determining key parameters such as action during AJAX requests. For example:

Function _ bbbb ()
{
If ($ _ POST ['action'] = 'bbbbbb ')
 {
$ Id = $ _ POST ['id'];
$ Json ['info'] = 'Rand = '. rand (0, $ id );
Header ("Content-Type: application/json ");
Echo json_encode ($ json );
Exit;
 }
}
Add_action ('init ',' _ bbbb ');

You can also do this:

Function _ bbbb ()
{
If ($ _ POST ['action'] = 'bbbbbb ')
 {
$ Id = $ _ POST ['id'];
$ Respond = 'Rand = '. rand (0, $ id );
Echo $ respond;
Die ();
 }
}
Add_action ('init ',' _ bbbb ');
2. Efficiency of the two AJAX methods
Local network speed test results (sent data includes id and action, a small amount of data ):

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.