Home>
Assumptions and questions

-Display API search results within the site.
-I want to prevent continuous execution so as not to bother the API user.
・ WordPress.

Is there any good way?

conditions

The search is executed by AJAX, and both logged-in users and non-logged-in users are possible, so as followswp_ajaxAlsowp_ajax_noprivI also use it.

add_action ('wp_ajax_nopriv_my_ajax_search_api','my_ajax_search_api');
add_action ('wp_ajax_my_ajax_search_api','my_ajax_search_api');
function my_ajax_search_api () {
  // Return search results using api
}
draft

At this time, I came up with the following method, using a nonce issued to any user.

① Issue a nonce to the user when visiting the site

② User executes search

③ Save nonce and execution time in your own table

④ Stop processing if the execution time of the nonce is within 1 second, and start searching if it is 1 second or more.

⑤ Echo the result to JS

However, this method does not fit well in that you have to create your own table for ③.

Or ③ ​​instead of your own tablefile_put_contentsI thought about the idea, but after all ... what about?

I think there is a more convenient or appropriate method, and I am seeking the opinions of experts.

Thanks for your advice.

  • Answer # 1

    Should I put it in $_SESSION?