ajax - How to use several wp_ajax_ functions for different queries?

I am working on getting multiple functions to work with wp_ajax_ and the first two functions fire just fine. However I h

I am working on getting multiple functions to work with wp_ajax_ and the first two functions fire just fine. However I have the need for a completely new query that will take advantage of AJAX also.

  • Should I beef up my first function with conditionals? Or
  • Can I init more than one wp_ajax_ function like my code below?

My code so far:

add_action( 'init', 'my_ajax_init' );
function my_ajax_init() {
   add_action('wp_ajax_nopriv_wpa56343_search', 'my_ajax_search');// works
   add_action('wp_ajax_wpa56343_search', 'my_ajax_search'); // works

   add_action('wp_ajax_nopriv_nogeo_results', 'nogeo_search'); // does not
   add_action('wp_ajax_nogeo_results', 'nogeo_search'); // does not
}

my_ajax_search is functioning fine with my JS AJAX calls. nogeo_search is not. My question is - is what I am doing to declare my ajax functions correct? If it is not correct please explain how to use wp_ajax_ with multiple callback functions.

I am working on getting multiple functions to work with wp_ajax_ and the first two functions fire just fine. However I have the need for a completely new query that will take advantage of AJAX also.

  • Should I beef up my first function with conditionals? Or
  • Can I init more than one wp_ajax_ function like my code below?

My code so far:

add_action( 'init', 'my_ajax_init' );
function my_ajax_init() {
   add_action('wp_ajax_nopriv_wpa56343_search', 'my_ajax_search');// works
   add_action('wp_ajax_wpa56343_search', 'my_ajax_search'); // works

   add_action('wp_ajax_nopriv_nogeo_results', 'nogeo_search'); // does not
   add_action('wp_ajax_nogeo_results', 'nogeo_search'); // does not
}

my_ajax_search is functioning fine with my JS AJAX calls. nogeo_search is not. My question is - is what I am doing to declare my ajax functions correct? If it is not correct please explain how to use wp_ajax_ with multiple callback functions.

Share Improve this question edited Sep 30, 2019 at 7:45 Eje 1654 bronze badges asked Aug 13, 2013 at 21:05 Ben RacicotBen Racicot 1,4363 gold badges18 silver badges27 bronze badges 7
  • I cannot get any code to format above. So sorry – Ben Racicot Commented Aug 13, 2013 at 21:09
  • The markdown (among other things) sometimes has trouble with code that follows a list. I added a line of "normal" text to force it to behave. – s_ha_dum Commented Aug 13, 2013 at 21:47
  • Your code is fine. The second question is a matter of preference and the first (and last) are not answerable, in my opinion, without more information. – s_ha_dum Commented Aug 13, 2013 at 21:51
  • The conditional question was based on if using multiple wp_ajax_ functions is a bad idea. I'd be forced to send extra post vars etc to conditional my existing query. Having trouble getting all this to output... Thanks for the good answers. – Ben Racicot Commented Aug 13, 2013 at 23:29
  • 1 Mean? No idea what you mean. Your question/problem is not clear to me. – s_ha_dum Commented Aug 14, 2013 at 0:01
 |  Show 2 more comments

1 Answer 1

Reset to default 2

I was able to get multiple wp_ajax_ functions to declare callback functions like this:

add_action( 'init', 'my_ajax_init' );
function my_ajax_init() {
    add_action('wp_ajax_nopriv_wpa56343_search', 'first_search');
    add_action('wp_ajax_wpa56343_search', 'first_search');
}

add_action( 'init', 'my_ajax_no_geo_init' );
function my_ajax_no_geo_init() {
    add_action('wp_ajax_nopriv_nogeo_results', 'second_search');
    add_action('wp_ajax_nogeo_results', 'second_search');
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745119415a4612325.html

相关推荐

  • ajax - How to use several wp_ajax_ functions for different queries?

    I am working on getting multiple functions to work with wp_ajax_ and the first two functions fire just fine. However I h

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信