wordpress use single ajax in place of multiple ajax requests in a smarter way

I appreciate your question is quite old and you no doubt already have your solution but hopefully this may help someone else. You could replace your javascript post calls with a single call to a single function that extracted both parts of data and returned them as an object. $(‘body’).on(‘change’,’#product_name’, function() { //Get product prices … Read more

Update Data parameter of a wp_localize_script() call

wp_localize_script results in your data being printed to the page before your script tag via PHP, whether it be in wp_head or wp_footer, depending on where you’ve set your script to be output. Calling wp_localize_script from an AJAX handler won’t do anything, you’re returning data via JavaScript. What you can do is put any data … 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

Load Post into DIV with Ajax

First of all you need to register a action callback for you ajax request. Secondly you need to send all ajax request to wp-admin/admin-ajax.php (both GET and POST). And lastly you need to modify your javascript a little to pass the action parameter which will trigger the callback. and var post_link = $(this); doesn’t gives … Read more

wp ajax return 0

There’s a simple alternative that sidesteps this issue entirely with a modern and easy to use interface, just use a REST API endpoint! So lets turn this: add_action(‘wp_ajax_nopriv_linked_post_image_grid’, ‘linked_post_image_grid’); add_action(‘wp_ajax_linked_post_image_grid’, ‘linked_post_image_grid’); function linked_post_image_grid(){ echo ‘this is return message’; die(); } Into this: add_action( ‘rest_api_init’, function () { register_rest_route( ‘buxbeatz/v1’, ‘/linked_post_image_grid/’, array( ‘methods’ => ‘GET’, ‘callback’ … 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