Three level taxonomy dropdown frontend

After spending many hours using google I managed to make 3-level hierarchical taxonomy dropdown with ajax. Here is my code: searchform.php <script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js” ></script> <script type=”text/javascript”> $(function(){ $(‘#apskritis’).change(function(){ var $apskritisSlug=$(‘#apskritis’).val(); // call ajax $(“#savivaldybe”).empty(); $(“#miestas_kaimas”).empty(); $.ajax({ url:”/wp-admin/admin-ajax.php”, type:’POST’, data:’action=get_savivaldybes&apskritis_slug=’ + $apskritisSlug, success:function(results) { //alert(results); $(“#savivaldybe”).removeAttr(“disabled”); $(“#savivaldybe”).append(results); } }); } ); $(‘#savivaldybe’).change(function(){ var $savivaldybeSlug=$(‘#savivaldybe’).val(); // … Read more

Load ajax if is_home()

Use: if ( is_home() or ( defined( ‘DOING_AJAX’ ) && DOING_AJAX ) ) is_home() returns TRUE on the front-end only, admin-ajax.php on the other hand is an admin page. So you need to check both conditions.

Using AJAX with Forms

Your add_action() calls for the AJAX handlers are too late. Add these hooks earlier, the best action is probably wp_loaded: add_action( ‘wp_loaded’, ‘register_ajax_handlers’ ); function register_ajax_handlers() { add_action( ‘wp_ajax_jp_ajax_request’, ‘jp_ajax_process’); add_action( ‘wp_ajax_nopriv_jp_ajax_request’, ‘jp_ajax_process’); } See also: Debug AJAX. This code should be placed in a plugin or in your theme’s functions.php.

How to use several wp_ajax_ functions for different queries?

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’); }

Using foreach inside an ajax function

Ok so it actually looks like there is a couple of ways you could do this. The first one is by changing your $list into an array and returning it as JSON. It will be easy to change $list to an array by just adding it like this: $list[] = ‘<option value=”‘.$term->term_id.'”>’.$term->name.'</option>’; Then return it … Read more

Using ajax with wordpress

In WordPress Ajax works this way: First, you register the ajax action and the function you want to use to serve that action, of course, you have to write that function. All the request go to the same URL wp-admin/admin-ajax.php, what it changes is the action, and depending on the action, a different function is … Read more

admin-ajax.php slow, how to speed it up?

Ajax endpoint is known to be relatively slow, but that primarily comes from loading WordPress core. If you are seeing drastic difference between two endpoints (native Ajax vs yours custom) that both perform core load, then something is doing something that degrades Ajax endpoint on top of it. I would profile Ajax endpoint to have … Read more

Placement of add_action() for ajax callback?

When you send an AJAX request, wp-admin/admin-ajax.php is called, not your template file. This separate request has no knowledge of all existing code in all existing templates, because they aren’t even included. What is included: the theme’s functions.php and plugin files. This is why your callback works in a plugin. As a rule of thumb: … Read more

I have to post data by AJAX in wordpress to another Website

This will not work due to the “same-origin policy” unless that server you are trying to reach specifically allows these type of requests with something like Access-Control-Allow-Origin: *. You can read the documentation on MDN in order to get more information about this topic. From my tests, the server will also need another header configured … Read more