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
- Google Maps API throws “Uncaught ReferenceError: google is not defined” only when using AJAX
- Initialize TinyMCE editor / visual editor after AJAX insert
- Displaying PHP Errors from admin-ajax.php
- wp_set_auth_cookie() doesn’t work in Ajax call
- WP-AJAX vs WP REST API: What to use for requests to the website from outside?
- Is it safe to assume that a nonce may be validated more than once?
- Using Nonces for AJAX that only retrieves data
- store/cache ajax sent data to avoid repeated request
- How to implement AJAX post navigation into WordPress?
- Run again current query via ajax but changing a var
- Quick Edit: Selected Custom Taxonomy Not Refreshing After Save
- Is it OK to use a system cron to trigger a function hooked into the AJAX API
- wp_verify_nonce always returns false when logged in as admin
- fetching via fetch/ajax gutenberg block data from third party
- Conditional action hooks
- Is it safe to use $_POST directly in my plugin instead of using admin-ajax.php to receive data from ajax?
- How to make get_theme_mod work with AJAX in the customizer preview
- How to check an ajax nonce in PHP
- admin-ajax.php slows down, but cant figure out which plugin
- Fatal error: Call to undefined function get_post() with ajax
- Convert this relative path to absolute
- Load WP content with ajax less server demanding and faster than normal page loading?
- WP Admin AJAX Security – using POST to include a relative URL
- Admin-ajax.php 400 error
- wp_create_nonce() in REST API makes user->ID zero
- Plugin: AJAX query external API to sync to tables
- splitting the URL using jQuery
- How do I get reusable blocks via frontend REST API?
- AJAX Load more on CPT returning random posts
- WordPress admin-ajax.php not available for subscribers/contributors
- WP Ajax never returning any data / calling action
- get_queried_object_id in AJAX
- Should I check for privileges before hooking into `wp_ajax_$handle` or after?
- Refresh Markercluster after ajax call
- Mapping Backbone Models to serialized array in wp_options table
- WordPress Settings api data not sanitized if i use ajax
- Images loading over http instead of https
- Ajax login without redirect/reload
- wp_mail 200 response but no mail sent
- Ajaxify Form That Submits To Same Page To Display Post Data [closed]
- Ajax call with javascript in post content (not enqueued)
- Change header.php after ajax call
- What’s the latest I can hook into wp_ajax_%?
- Retrieve JSON file from JS trough php
- Nonce doesn’t validate in nopriv call
- Using admin-ajax prevents regular php form submission
- Sql formatting for post data within function
- Get returned URL from wp_remote_post if response code is 302
- Change URL without reload Ajax
- Admin-ajax.php 400 error in custom theme
- Why does check_ajax_referer give a 403 error on https websites?
- Hide Load more Ajax button if there is no more users to load or less than the number?
- Send checkbox status with Ajax / JSON and save it
- ajax refresh to display new posts
- wp ajax execution time aborted 30 sec
- How to get setting from separate file?
- Contact form – ajax, wp_mail
- WordPress function not defined in AJAX call
- admin-ajax.php calls fail if referrer is sent with 500
- Fetch value using AJAX in WordPress
- AJAX not working when clicking load more button, when two terms are present in tax_query
- AJAX – get_posts for a specific post type returns empty
- Admin Ajax always return 0
- WPBakery post content inside Bootstrap Modal
- error_log() not working within wp_ajax_{action} handler
- Hook after all metadata are set is triggering several time
- check_ajax_reffer not working when logged
- Assign author on ajax wp post insert
- wordpress admin ajax trash_comment
- Its possible to ajax update the content of the admin custom post type list
- WordPress custom ajax login not working on mobile browsers
- Failed to load resource: the server responded with a status of 404 (Not Found) admin-ajax.php
- Javascript output now showing in custom widget
- Create a post with REST API and adding a category
- how to correctly use json_encode with ajax
- How to trigger lost password email using REST API?
- Display post thumbnail after clicking on post id with AJAX
- Using AJAX on Contact-form the WordPress way
- Retrieve Header Background Image with AJAX
- How to block search engines indexing certain AJAX actions
- AJAX call in backend results in empty update_option
- Ajax Comment: Page reloads whenever the comment submission form is reloaded
- wp_ajax_action, wp_ajax_nopriv_action not working
- get_comments() returns empty array if called through AJAX
- Manually cache a special, non-WP-but-using-WP page (e.g. Ajax results) with W3TC
- Is it a good idea to init ajax functions only while ‘DOING_AJAX’
- How to get parent comment’s id?
- Static var overwritten with WP AJAX action, works fine without AJAX
- using ajax to query sql
- Front-end ajax problem all users and guests getting a 302 redirect when accessing wp-admin/admin-ajax.php
- Use Ajax to get an Options Settings Value