Sending an AJAX request to a specific file is not the way to do AJAX in WordPress. This is because WordPress itself is not loaded by your file, so you wouldn’t be able to use WordPress functions without an error.
There are two proper ways to handle AJAX requests in WordPress:
- Using the
wp_ajax_hooks: https://developer.wordpress.org/plugins/javascript/enqueuing/#ajax-action - Creating a custom REST API endpoint: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
Related Posts:
- Initialize JS with an ajax loaded ACF form
- Filter WP user acf field by ajax
- Is there a way to optimize function that is used for returning data in an ajax-call?
- Populating content dynamically via AJAX and Advanced Custom Fields [closed]
- post values to custom post type which has advanced custom fields
- Delete ACF repeater field row on Front End
- Filter custom post types by custom field using AJAX
- Merge PDF files from post custom fields(ACF) into one PDF file and respond back the created PDF file, WordPress(AJAX Request)
- Ajax call undefined index
- Best way to end WordPress ajax request and why?
- Why use wp_send_json() over echo json_encode()?
- Why not register shortcodes if is_admin dashboard?
- How to pass data parameter to ajax action function handler
- Multiple ajax nonce requests
- Update user meta using with ajax
- WordPress function that makes HTML safe to be sent via AJAX request
- admin-ajax.php returns 0. How do I debug it and fix it?
- How to properly use wp.ajax.post?
- AJAX vs Fragment Caching for W3 Total Cache [closed]
- Basic question on passing variables to AJAX in WordPress
- Delete row of custom table in WordPress using AJAX
- Change theme based on window size
- Serving nonces through AJAX is not refreshing nonce, returning 403 error
- Cannot update my options using wp_ajax
- Class WP_Rewrite not found
- Prevent wp_debug from polluting ajax within plugin
- How to localize inline script called with ajax
- Conditional Ajax inclusion
- How to allow to user non logged in WP system upload in media library?
- WordPress function to get term or post object by id
- Allow AJAX call to other roles than admin
- submitting form via admin-ajax.php returns 0
- Help add ajax load more button
- Conditional success callback based on Ajax Response not working
- Can a wp_nonce created from domain 1 to be verified on domain 2?
- How to use several wp_ajax_ functions for different queries?
- Three level taxonomy dropdown frontend
- I’m getting a 401 while calling the wp-json endpoint via ajax
- How to solve “Updating failed” with 403 Error when trying to save a Site or Post in Gutenberg?
- Keep getting 400 Bad Request error when trying out AJAX on custom theme
- How to set post_id to 0 when you upload image via Add media button. (async-upload.php)
- Ajax simple experiment
- How to load new text widgets and polls into the sidebar without reloading the page?
- Updating User Meta with Array on Click of Button AJAX
- Why is on status span no search results?
- eventON plugin AJAX call triggers website crash
- Admin ajax add tag callback
- Stop auto load wp-admin admin-ajax.php
- MySQL select query with ajax
- Exposing data using a custom WordPress API
- Use WP admin AJAX url to hide API key
- ajax form is returning the dreaded “[HTTP/1.1 400 Bad Request” and a zero
- 400 Bad Request when sending XHR from React.js to admin-ajax.php
- How can I wp_send_json data?
- Making POST request with AJAX returns a 400 error (without jQuery)
- Load image src through Ajax by ID?
- Using get_theme_mod in php ajax form doesn’t work
- PHP session when called wp_ajax_nopriv
- What exactly does ‘authenticated’ mean for wp_ajax_nopriv?
- Use AJAX in a WordPress Plugin to Get Data From Custom Database?
- How to make image in TinyMCE clickable
- How to use Jeditable plugin with admin-ajax.php?
- Ajax mousedown call getting “cancelled” when clicking link
- WordPress Ajax Not Firing
- Get uploaded attachment width & height and attachment ID after upload them
- Uncaught TypeError: Cannot read properties of undefined (reading ‘message’) [closed]
- Disabling ajax code that does a POST request that ends in a 400 error code
- Refresh checkout fields on add to cart with order bump
- Add Server Side validation in Ajax mail form
- Is it good practice to use REST API in wp-admin plugin page? [closed]
- How to prevent my external API call from being called by anyone but me (my site)
- Ajax not sending the data correctly- Multidimensional array
- get a bad request 400 on my ajax-admin.php file
- Ajax call works for logged in users and returns “Bad Request” for guests [duplicate]
- On form submitting, admin-ajax.php shows status as pending
- How to force the admin-ajax.php file to load over HTTPS?
- Unable to ajax request with meta_query
- get_body_class() in ajax envrionment
- Should I edit a user meta field with PUT, PATCH, or POST and WP::Editable
- How To Pass Theme Path In Javascript Ajax to Pass Id On another Page in WordPress Custom Page Template?
- AJAX action returning empty posts array?
- Cannot add multiple single images at once
- How to send custom variable in Media Uploader ajax request?
- WordPress Ajax JSON success return no being recognized
- Session variables lost during Ajax calls – WordPress – Sage Starter Theme
- WordPress page content only in fancybox except direct click
- Ajax takes 10x as long as it should/could
- While doing ajax, WordPress returning internal server error 500 and stating that my theme functions are undefined
- SHORTINIT and sessions
- Ajax navigation and scripts loaded only on certain pages
- using Ajax: call to undefined function get_option
- WordPress Pagination with ajax – Dots
- Bad Request when adding new post via ajax form
- How To Pass Ajax Value to PHP Query?
- Ajax load more posts button
- ajax for visitors wordpress
- Access to a data from a response AJAX called in a template file php
- Escaping admin_url output being passed to js (esc_js vs esc_url)
- Uncaught ReferenceError: custom_ajax is not defined
- wp-admin/admin-ajax.php 400 Bad request (chrome console)