Simple answer: Don’t add a separate PHP file for AJAX requests. Declare the functions that will be used in your plugin and load them via admin-ajax.php
.
add_action('wp_ajax_custom_action_name','custom_action_handler');
// to make AJAX action available for non-logged in users:
add_action('wp_ajax_nopriv_custom_action_name','custom_action_handler');
function custom_action_handler() {
global $wpdb;
// optionally keep the actual functions separately
// include(dirname(__FILE__).'/ajax-handler.php');
// custom_ajax_handler_process(); // handle request
// or just handle request here
exit;
}
$endpoint = admin_url('admin-ajax.php').'?action=custom_action_name';
WordPress will automatically take the action
and match it to the wp_ajax_
(and/or wp_ajax_nopriv_
) hook suffix to load the hooked function (custom_action_handler
).
Complex answer: you can have a separate PHP file to handle the AJAX requests, but you need to be able to load the WordPress functions and classes you need (eg. for $wpdb
) within it first. You can do this by:
define('SHORTINIT',true);
- Require
wp-blog-header.php
from WordPress root path. - Include any needed WordPress includes manually.
- Process the AJAX request.
Step 3 is the hard part as depending on what you are doing you may need a number of inter-related functions for the file to even load without a fatal error
crashing your code and thus request response. Though if you are just accessing the database functions/class then these may be minimal. Overall this is a doable approach, but may mean much more work for only a slight increase in AJAX response performance time, so probably the simple answer is better.