How to add ajax url to js using wp_add_inline_script()?

If anyone still needs it: wp_add_inline_script( ‘map-scripts’, ‘const ajax_info = ‘ . json_encode(array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’), )), ‘before’ ); but, it’s good practice to use nonce as well for better security: wp_add_inline_script( ‘map-scripts’, ‘const ajax_info = ‘ . json_encode(array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’), ‘nonce’ => wp_create_nonce(‘your_nonce_handler’), //the more specific the better ‘your_other_item’ => “some additional data”, … Read more

admin-ajax Firing Error 400 When Logged In

Did I set up that wp_ajax_nopriv_ hook wrong? No, you did not. But that hook is for logged-out or unregistered users only. For logged-in users, the hook is wp_ajax_<action>. So you just need to change the add_action(‘get_events’ in your code to add_action(‘wp_ajax_get_events’, and the error 400 would be gone: add_action(‘wp_ajax_get_events’, ‘get_events_func’); // for logged-in users … Read more

unable to use ‘new WP_Query’ in AJAX call

Just to clear this up for someone that might have similar problem, everything was find with query, the problem with this was WordPress was looking for WP_Query class in namespace file, replacing new WP_Query with new \WP_query solved this problem 😀 thanks for help!

Ajax call on new site with jupiterx theme getting 400 response [closed]

You will need to use and pass a nonce when accessing admin-ajax Enqueue script with nonce: $params = array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’, $protocol), ‘ajax_nonce’ => wp_create_nonce(‘any_value_here’), ); wp_localize_script( ‘my_blog_script’, ‘ajax_object’, $params ); ajaxurl – This is the absolute address, taking into account http:// or https://, to your ajax processing script. This script is in the … Read more

Contact form 7 Hide response messages after 5 seconds

you can use below code to hide message // Contact Form 7 submit event fire document.addEventListener(‘wpcf7submit’, function(event) { setTimeout(function() { jQuery(‘form.wpcf7-form’).removeClass(‘sent’); jQuery(‘form.wpcf7-form’).removeClass(‘failed’); jQuery(‘form.wpcf7-form’).addClass(‘init’); }, 1000); }, false);

AJAX action through direct link

Use WordPress’ generic POST/GET handler wp-admin/admin-post.php Don’t let the name confuse you – it accepts both GET/POST, and just like admin-ajax.php it accepts both authorised (admin) and non-authorised (public) requests. function wpse_406199_custom_action() { // Do something! // Maybe redirect back to where they came from? wp_redirect( wp_get_referer() ); exit; } // Non-logged-in requests add_action( ‘admin_post_nopriv_wpse_406199_custom_action’, … Read more

Custom AJAX Endpoint not returning any result

You need to use the defined namespace as the prefix for the add_action callbacks so that PHP knows which function it is supposed to call. More on this here, add_action in namespace not working The add_actions should look like this then, add_action(‘wp_ajax_ajax_endpoint’, __NAMESPACE__ . ‘\\ajax_endpoint’); add_action(‘wp_ajax_nopriv_ajax_endpoint’, __NAMESPACE__ . ‘\\ajax_endpoint’); When doing development work it is … Read more

Can’t get AJAX call working in custom plugin

So the AJAX action defined in Myplugin::define_admin_hooks() has been corrected (it’s just a typo in the question). But there’s another issue in your code, also in the above function, where the second parameter for $this->loader->add_action() should actually be $plugin_admin (which is an object and an instance of the Myplugin_Admin class) and not wp_ajax: Incorrect: $this->loader->add_action( … Read more

Ajax future single post query doesn’t work when NOT logged in

usually it because you are missing an “add_action” call for none logged in users. add this to your plugin or functions.php add_action(‘wp_ajax_nopriv_REPLACETHIS’, ‘ajax_get_date_post’); and change the REPLACETHIS to your action value from the ajax call. remember that when calling ajax from front-end and you want to allow none logged in users to make these calls … Read more