Ajax call does not work for this simple code

You have a space in your function name on the action call, add_action( ‘wp_ajax_nopriv_ MyAjaxFunction’, ‘MyAjaxFunction’ ); add_action( ‘wp_ajax_ MyAjaxFunction’, ‘MyAjaxFunction’ ); …should be, add_action( ‘wp_ajax_nopriv_MyAjaxFunction’, ‘MyAjaxFunction’ ); add_action( ‘wp_ajax_MyAjaxFunction’, ‘MyAjaxFunction’ );

Getting a variable using $post ajax back from php to js response in WP

You need to send the var through your ajax request: $.post( //be suer that this var is pointing to wp-admin/admin-ajax.php WPaAjax.ajaxurl, { action : ‘my_php_action_to_do’, //here your var and its value my_var : ‘the_value’ }, function( response ) { response = jQuery.parseJSON(response); alert(response.message); } ); In your my_php_action_to_do function: function my_php_action_to_do(){ $var[‘message’] = $_POST[‘my_var’]; //Send … Read more

Ajax call with javascript in post content (not enqueued)

If you add the script inline you don’t need to use wp_localize_script. All you have to do is directly print your inline script exactly as you need it dinamically. For example, in your template: <?php // The PHP code of the template here ?> <script> jQuery(document).ready(function($){ var s=1; $(‘button’).on(‘click’,function() { //wfire the ajax call jQuery.ajax({ … Read more

ajax is returning 0

You need to follow these steps to properly call ajax function in WordPress, Register and localize your script, eg. wp_register_script( ‘*handle_name*’ , *js_file_path* , array( *dependency_name* ) , true , true ); It will load your script at page footer. Refer to the following link: https://developer.wordpress.org/reference/functions/wp_register_script/ wp_localize_script( ‘*handle_name*’ , ‘*a_unique_name_to_call_this_object*’ , array( ‘ajaxurl’ => admin_url( … Read more

POST Ajax bad request

I tested your code and got it to work by removing the following line as it is unnessacary. options.data = JSON.stringify(options.data); You can also safely remove the following lines in your AJAX call. contentType: ‘application/json’, // tried without it too dataType: ‘json’, // tried without it too Please check out AJAX in Plugins on the … Read more

Ajax Multi Response Problem

As mentioned in the documentation: When the handler has finished all of its tasks, it needs to die. If you are using the WP_Ajax_Response or wp_send_json* functions, this is automatically handled for you. If not, simply use the WordPress wp_die() function. wp_die(); // That’s all folks! So you either need to use: $result[‘success’] = false; … Read more