I am trying to show subcategories of a category in WordPress using AJAX: when I select a main category, there is a call to WP Ajax and the result is used in showing the subcategories.

So far, I have the client-side code that works when not calling a WP function (this code is in a theme page):

jQuery('#cat-location-main').change(function () {
    var optionSelected = jQuery(this).find('option:selected');
    var valueSelected = optionSelected.val();
    var textSelected = optionSelected.text();
      type: 'POST',
      url: ajaxurl,
      data: {
            action: 'myajax-get-subcat',
            category: valueSelected,
            // send the nonce along with the request
            categoryNonce: '<?php echo wp_create_nonce( 'myajax-get-subcat-nonce' );?>'
      success: function(data, textStatus, jjqXHR) {
      dataType: 'json'

And I have this in the functions.php:

add_action('wp_ajax_myajax-get-subcat', 'myajax_get_subcat');

function myajax_get_subcat() {
    $nonce = $_POST['categoryNonce'];
    $main_category = $_POST['category'];

    if (!wp_verify_nonce($nonce, 'myajax-get-subcat-nonce'))
        die ( 'Busted!');

    if(function_exists('wp_dropdown_categories')==true) {
        echo 'true';
    } else {
        echo 'false';

Now I get a "true" on the client side when commenting wp_dropdown_categories line, and I get absolutely nothing when I uncomment that line (PHP crash). Nothing in php error log (WAMP setup).

Also, not working even if I add require_once(__DIR__.'/../../../wp-load.php'); but it works if I use GET in browser (for the functions.php). Any help would be greatly appreciated!

  • What happen when you call the function with minimum arguments - wp_dropdown_categories('taxonomy=category') ?
    – Shazzad
    Apr 8, 2015 at 20:25
  • Nothing, it's not running at all, the same issue, tried with other functions as well: get_categories(), also not working. Apr 8, 2015 at 20:28
  • Weird, this isn't a regular behavior of WP as far i know. Try deactivating all plugins and then, try moving your code into a default theme such as 2012.
    – Shazzad
    Apr 8, 2015 at 20:31
  • If I crate a custom function in category-template.php and call that function instead wp_dropdown_categories it's also not working (even if I don't have nothing in that function, it's empty). Apr 8, 2015 at 20:44
  • a function in a template file won't work with an ajax request, the template file is never loaded in that request.
    – Milo
    Apr 8, 2015 at 21:01

2 Answers 2


Your problem is probably because you do not return a json object but an html (actually mixed text and html), and you set jQuery to validate that the response is json, which it isn't.

your code at the ajax handler should be something like

$catshtml = wp_dropdown_categories(.....echo=0);
$ret = array('data' => $catshtml);

on the browser side you need to look for the content of the data attribute of the json element you receive from the server.

Debugging tip: always look at what is actually transmitted as a response in the browser developer tools section first before starting to rely on the consol log.

  • Yes, this was the issue, thank you for helping me out! Apr 8, 2015 at 21:01

When I seemingly get nothing from an Ajax call I run Fiddler and examine the return when it comes to the request.

The other day an Ajax call was failing inexplicably. Upon examining the return I saw another programmer was dumping an array into the output before I received my response. I never would have guessed that because the JavaScript never hit the breakpoint I set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.