WordPress Insert not working with ajax

Your AJAX callback method should be outputting something followed by a die() statement. function wp_insert() { ..your code echo $whatever_your_results_are; // die(); } I would also recommend against prefixing your custom methods with wp_. That should be reserved for WordPress and will cause confusion to other developers – and probably you 6 months down the … Read more

What exactly does ‘authenticated’ mean for wp_ajax_nopriv?

Authenticated here means according to the is_user_logged_in() function, i.e. if the user is logged in or not, no matter the user role. This part from the admin-ajax.php file, explains it all: if ( is_user_logged_in() ) { /** * Fires authenticated AJAX actions for logged-in users. * * The dynamic portion of the hook name, `$_REQUEST[‘action’]`, … Read more

200 return code on ‘POST /wp-admin/admin-ajax.php’ while NOT logged in

wp-admin/admin-ajax.php is the script used by any plugin or theme using WP Ajax API and Ajax actions can be registered for non-logged in users. For example: //For logged in users add_action( ‘wp_ajax_my_action’, ‘my_action_callback’ ); //For non-logged in users add_action( ‘wp_ajax_nopriv_my_action’, ‘my_action_callback’ ); There is no problem on that. See WP Ajax documentation for more information.

update_option in WordPress AJAX

Your first chunk of code is PHP, the second is javascript. Here is how a full example might look: In PHP (your theme’s functions.php file or a plugin file): //First enqueue your javascript in WordPress function your_prefix_enqueue_scripts(){ //Enqueue your Javascript (this assumes your javascript file is located in your plugin in an “includes/js” directory) wp_enqueue_script( … Read more

WordPress API functions not working at AJAX functions.php call

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); wp_send_json($ret); die(); on … Read more

How to request admin-ajax.php correctly when wordpress URL and site URL are different?

admin_url() should be used. However for some websites this request gives error 404 in front-end admin_url(‘admin-ajax.php’) should point to an actual file (admin-ajax.php), as such WordPress won’t pass it through index.php, and so it shouldn’t lead to the theme’s 404 page. (Unless the file has been deleted). … unless you have a .htaccess file which … Read more