I’m getting a 401 while calling the wp-json endpoint via ajax
I had accidentally created a POST rather than a GET. Obviously, you need credentials if you post.
I had accidentally created a POST rather than a GET. Obviously, you need credentials if you post.
It’s important to exit the function when using the wp_ajax hooks. Hopefully this fixes it. function get_ticket_price() { $ticketID = $_REQUEST[‘ticketID’]; echo wc_get_product( $ticketID )->get_price(); // Don’t forget to stop execution afterward. wp_die(); } add_action( ‘wp_ajax_get_ticket_price’, ‘get_ticket_price’ ); add_action( ‘wp_ajax_nopriv_get_ticket_price’, ‘get_ticket_price’ );
wp_send_json() already converts the value to JSON, so you don’t need to do it again. You can remove wp_json_encode().
But once I try to look for other meta values, the return bring all members in the DB. Are you sure the meta other than name did not work, i.e. all members/users were returned? Because as I could see it, for your keywords, country and disciplines meta, so long as your compare value is good, … Read more
I figured it out. Instead of trying to combine the two functions together in my functions.php file, I added both of them. Why did this work? Because one is adding a new comment (via a call to ajax-comments.php) and the other function is querying all the existing comments. So it’s something like this: My original … Read more
Well you want to hook into the wp_loaded action. Do the stuff you need to do and then exit the process. You should probably look at http://codex.wordpress.org/AJAX_in_Plugins
Natively WordPress posts are subset of HTML (even more like semi-HTML – paragraph tags can be implied and added on output, but not stored). As result it really really doesn’t like to store or process active code in post’s content. This applies both to internal logic in PHP and front-end logic of post editor (it … Read more
The condition for such hook to fire is quite simple, as per admin-ajax.php: if ( !empty( $_REQUEST[‘action’] ) ) do_action( ‘wp_ajax_nopriv_’ . $_REQUEST[‘action’] ); So the likely reasons are: You script does not pass action name properly (which is unlikely if exactly same code works for logged in user). Action name gets messed up in … Read more
I understand the problem you are having, I was thinking of the same thing. It is not really a problem tho, it’s more a matter of good programming design and have a good separation of front-end and back-end. Now the way I have managed to solve this is doing the following: I hope you are … Read more
The answer is quite simple – you are not giving admin-ajax what it is expecting. Please refer to the source code http://core.trac.wordpress.org/browser/tags/3.2.1/wp-admin/admin-ajax.php#L17 if ( ! isset( $_REQUEST[‘action’] ) ) die(‘-1’); So make sure you are giving it an action (which you are not). Then, a little lower there is: if ( ! $id ) die(‘-1’); … Read more