admin-ajax.php HTTP400: BAD REQUEST – The request could not be processed by the server due to invalid syntax

You can’t send the data to admin-ajax.php as JSON: data: JSON.stringify({ action: “process_coupon” }), For the wp_ajax_ actions to be fired, WordPress checks $_REQUEST[‘action’], but this is not populated by PHP when the form is submitted as JSON. The correct way to send the request with jQuery is to pass the data like this: data: … Read more

add_action(‘wp_ajax_[action name]’, myfunction) problem

There are only three explanations for why this isn’t working: First, something could be unregistering your action. That doesn’t seem very likely, though. Second, your code never runs in the ajax initialization. Third, the wp_ajax_my_function_call hook never runs. I wouldn’t worry about the possibility of the first. As far as the second, just make sure … Read more

Creating custom AJAX requests

In WordPress the way of handling Ajax calls is a bit different the plain PHP but still very simple, all ajax calls should be to wp-admin/admin-ajax.php and you just define your own functions by hooks, Take a look at What’s the preferred method of writing AJAX in WordPress

Load comments per post on click with AJAX

Aha, I think my syntax was wrong. I changed: action: ‘do_ajax’, data: { ‘post_id’ : ’72’ //using a post id that I *know* has comments, for testing! }, to: data: { ‘action’ : ‘do_ajax’, ‘post_id’ : ’72’ }, and I’m getting the correct response from the console.log which shows: [{“comment_ID”:”1″,”comment_post_ID”:”1″,”comment_author”:”Mr WordPress”,”comment_author_email”:””,”comment_author_url”:”http:\/\/wordpress.org\/”,”comment_author_IP”:””,”comment_date”:”2012-08-28 19:55:20″,”comment_date_gmt”:”2012-08-28 19:55:20″,”comment_content”:”Hi, this is … Read more

Creating a Front-end based User Search

As you haven’t offered the details on what you’re after, I’ll try to grab them all very briefly. Use the API – public WP_User_Query API functions Basically get_user_by() should be enough for you. Let’s say you fire of your form and the input field name was user_id. So you’d just retrieve the value from the … Read more

Elegantly using JavaScript on widget admin forms

One way around this I’ve discovered since posting the above question is to instantiate inline via WP_Widget::form(), with wp_enqueue_script() loading the necessary JavaScript (exactly as mentioned above) — except, and here’s the key part, load the JavaScript in the header instead of the footer (fifth argument of wp_enqueue_script()). Which makes a lot of sense now … Read more

Ajax returning correct data BUT at wrong place

You are specifying the data type returned from the AJAX call to be JSON. However, the string you’re returning (“batman begins”) is not in JSON-format. Thus, the jQuery ajax-call will fail. Try passing your data as JSON using json_encode: function locationContent() { echo json_encode( ‘batman returns’ ); die(); } This will fix your issue. Furthermore, … Read more