Ajax Request for both logged and non logged users

You can add the function to both hooks: add_action(‘wp_ajax_ajaxTest’, ‘ajaxTest’); add_action(‘wp_ajax_nopriv_ajaxTest’, ‘ajaxTest’); But, there’s a better way to do it, use a REST API endpoint: add_action( ‘rest_api_init’, function () { register_rest_route( ‘yourstuff/v1’, ‘/test/’, array( ‘methods’ => ‘POST’, ‘callback’ => ‘yourstuff_test_endpoint’ ) ); } ); function yourstuff_test_endpoint( $request ) { $stuff = $request[‘stuff’]; return “Received “.esc_html( … Read more

Bad request 400 from custom ajax call

There’s only one reason you get a 400 error with admin-ajax.php: The wp_ajax_{$action} or wp_ajax_nopriv_{$action} action has not been hooked with an {$action} that matches the action data parameter sent with your request. So either your callback is not hooked, or you’re not sending the action correctly. Your code is making both errors. You are … Read more

AJAX call – failling to call do_action

Aside from @Tareq answer (which is your main problem), you should wp_register_script and the wp_enqueue_script load your local copy of jQuery instead of the one from Google CDN localize your script using wp_localize_script

Ajax submit comments

you might find these links handy if you’re going to code it yourself http://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress/ http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/ I am fairly sure your URL is wrong url: “../wp-comments-post.php” – ajax in wordpress has to be sent (quite strangely) to/through admin-ajax.php (even if it’s not on the admin side). If you read the first link it is explained in … Read more

Would to use AJAX to get an option from the database and use it in a jquery setup or is there an alternative to consider?

Enqueue the script as you would normally, & then call the JS function right after the slider HTML output (or on wp_footer), and pass a JSON config back to the function. <!– slider HTML –> <script type=”text/javascript”> jQuery( “#my_slide” ).mySlider( <?php echo json_encode( $my_config /* array or object of arguments */ ) ?> ); </script> … Read more

ajax for filtering posts by category in wordpress loops

A piece of javascript/jquery: $(“#queryMenu ul li a”).click(function(){ var currentCategory = $(this).attr(‘id’); $.ajax({ type: ‘post’, url: ‘ajax-category.php’, //sometimes I’m using bloginfo to get current path: url: ‘<?php bloginfo(‘template_url’); ?>/ajax.php’, data: { currentCategory: currentCategory }, success: function(data) { console.log(data); } }); }); Put right path in url. Use $_REQUEST[‘currentCategory’] inside ajax-category.php to get category ID…

Auto-realod with new posts // Ajax call

If what you want is is a full page refresh then you don’t need ajax at all and inserting the htt-equip html meta will do the trick <meta http-equiv=”refresh” content=”5″ > (content is time in seconds IIRC) You just hook on wp_head and insert it. function wpse_228671_wp_head() { echo ‘<meta http-equiv=”refresh” content=”5″ >’; } add_action(‘wp_head’,’wpse_228671_wp_head’);