WordPress API functions not working at AJAX functions.php call -
WordPress API functions not working at AJAX functions.php call -
i trying show subcategories of category in wordpress using ajax: when select main category, there phone call wp ajax , result used in showing subcategories.
so far, have client-side code works when not calling wp function (this code in theme page):
jquery('#cat-location-main').change(function () { var optionselected = jquery(this).find('option:selected'); var valueselected = optionselected.val(); var textselected = optionselected.text(); console.log(valueselected); jquery.ajax({ type: 'post', url: ajaxurl, data: { action: 'myajax-get-subcat', category: valueselected, // send nonce along request categorynonce: '<?php echo wp_create_nonce( 'myajax-get-subcat-nonce' );?>' }, success: function(data, textstatus, jjqxhr) { console.log(data); }, datatype: 'json' }); });
and have in 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'; } wp_dropdown_categories('taxonomy=category&selected=1&echo=1&orderby=name&order=asc&hide_empty=0&hide_empty=0&hierarchical=1&depth=1&id=cat-location-secondary&child_of='.$main_category); exit; }
now "true" on client side when commenting wp_dropdown_categories line, , absolutely nil when uncomment line (php crash). nil in php error log (wamp setup).
also, not working if add together require_once(__dir__.'/../../../wp-load.php');
works if utilize in browser (for functions.php). help appreciated!
my problem because not homecoming json object html (actually mixed text , html), , set jquery validate response json, isn't.
ajax wordpress-plugin wordpress-theming wordpress
Comments
Post a Comment