Use the wordpress built-in ajax method

Source: Internet
Author: User

For example, if you click "Change" next to the logo on each page of this site, ajax will dynamically load one, using the built-in ajax method of wordpress. The following describes how to use the built-in ajax method of wordpress:


1. Add html and js to the header. Php file

The code is as follows: Copy code

Html:

<Span id = "random"> <I id = "say"> <? Php echo random_str ();?> </I> [<a href = "javascript: void ();" onclick = "say (); "rel =" nofollow "> Change One </a>] </span>

Js ajax request:

The code is as follows: Copy code
<! -- Change one -->
<? Php $ admin_url = admin_url ('admin-ajax. Php');?>
<Script type = "text/javascript">
Function say (){
JQuery (document). ready (function ($ ){
Var data = {
Action: 'say'
  }
$. Post ("<? Php echo $ admin_url;?> ", Data, function (response ){
$ ("# Say"). text (response );
});
});
}
</Script>

In wordpress, the url of ajax requests is unified and obtained using $ admin_url = admin_url ('admin-ajax. Php.

2. Write the receiving request processing function in function. php of the topic.

The code is as follows: Copy code
// Change one
Function say (){
Echo random_str ();
Die ();
}
Add_action ('WP _ ajax_say ', 'Say ');
Add_action ('WP _ ajax_nopriv_say ', 'Say ');
Add_action ('WP _ ajax_say ', 'Say ');
Add_action ('WP _ ajax_nopriv_say ', 'Say ');

The focus is on the two hook functions. The first parameter of wp_ajax_say is the name of the wp_ajax _ function, and wp_ajax_nopriv_say indicates the processing function of the user who has not logged on. The same is true for the user who has not logged on, so write them all.

In this way, the ajax function has been implemented, is it very convenient, but the ajax request of wordpress feels very slow !!!

It is best to attach a function with a random output famous saying:

The code is as follows: Copy code

Function random_str (){
$ Poems = array (
'You must have an employee. From the good, and from the bad. -- Confucius ',
'Becoming synonymous with excellence, many people do not need an environment of outstanding quality. -- Steve Jobs ',
'The living is to change the world. Is there any other reason? -- Steve Jobs ',
'Follow yourself. (Follow your heart) -- Steve Jobs ',
'The life is unfair; adapt to it. -- Bill gat ',
'Go your own way and let others say it. -- Dante ',
'Success is not a straight line, but a winding line. ',
'The thing that makes you sad, one day, you will surely say it with a smile. -- The salvation of Xiao Shenke ',
'If the forehead will be engraved with Zou Wen, you can only do Zou Wen, not in your heart. -- Chinese partner ',
'What is a dream? A dream is a thing that makes you feel persistence is happy. -- Chinese partner ',
'Remember that you are about to die -- jobs ',
'When you are happy, you want to think that happiness is not eternal. When you suffer, you think it is not eternal. ',
'The Ancients cloud: do your best to listen to your destiny. ',
'If life can be recovered to any previous version like svn ',
'Stay Hungry, Stay Foolish ',
'Do not feel tired when doing what you like ',
'Today is cruel, tomorrow is more cruel, and the day after tomorrow will be very beautiful, but most people will die tomorrow night-Ma Yun ',
'The Genius is 99% of the effort plus 1% of the inspiration, but there is no 1% of inspiration-Edison ',
'Don't be bound by dogma, it means you think like others, don't be overwhelmed by others' opinions, and listen to your intuition and spiritual instructions. -- Steve Jobs ',
'The clock hand can return to The original point, but it is not that of yesterday .',
);
Return $ poems [rand (0, count ($ poems)-1)];
 

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.