is_single() conditional check inside ajax php function

is_single returns if there is a single page displayed within the actual server process. Within the Ajax-Function, nothing is “displayed”, you’re doing Ajax. To check if the currently displayed object in your Browser is a single object, you need to transfer the information about this from the server process that serves your Browser to the … Read more

WordPress is Not Setting PHP $_POST on Custom Ajax

I’m having some doubts about your code. IF your using WordPress you should use https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) So you no session.php, but a function: add_action( ‘wp_ajax_foobar’, ‘my_ajax_foobar_handler’ ); function my_ajax_foobar_handler() { $u = $_POST[“url”]; $p = $_POST[“product”]; echo $u . ” ” .$p ; wp_die(); } and simplify yours JS jQuery.post( my_foobar_client.ajaxurl, { ‘action’: ‘foobar’, ‘url’ : … Read more

WordPress ajax-action how to return content

OK, so you misunderstood it a little bit, I guess… You do the first part correctly. So yes – AJAX requests should be sent to wp-admin/admin-ajax.php and the should have action param set in the request (either as a POST or as a GET). But then, you do it wrong. You register your action with … Read more

WordPress AJAX return 0 – My case

You function name and your ajax action callback name should be same. add_action(‘wp_ajax_pdfAction’,’pdfAction_callback’); add_action(‘wp_ajax_nopriv_pdfAction’,’pdfAction_callback’); function pdfAction_callback(){ $post = $_POST[‘pdfAction’]; echo “something”; wp_die(); } Please update your php code with the above. Currently, your php function is not getting executed. Update: The Ajax Script, $(document).on(‘click’, “#generate-pdf”, function(){ var price1 = $(“#price1”).val(); var price2 = $(“#price2”).val(); $.ajax({ … Read more

What’s the latest I can hook into wp_ajax_%?

There seems to be a big misunderstanding here about how hooks work, and how AJAX works. The reason that you can’t hook into AJAX in footer.php is because footer.php is not loaded during an AJAX request. No templates are. When you run add_action() you are scheduling a given function to fire when that action is … Read more

I can’t get a return value from Ajax

Returning the result array will only return to the PHP function calling it, instead you actually need to output it by echoing it, and in this case because it is an array, encoding it with JSON before outputting. So return $result becomes: echo json_encode($result); exit; and then add dataType to the javascript call so it … Read more

Get response for check_ajax_referer

It’s totally fine to manually call wp_die() which works fine both with AJAX and non-AJAX requests. Just make sure you return a valid response body; e.g. if your JS expects a JSON response, then return a JSON-encoded string. However, wp_send_json_error() is a wrapper for wp_send_json() which uses wp_die(), so you don’t need to call wp_die() … Read more