I writing a wordpress plugin, that connects to external database and fetch data. Also it create custom button for vitual editor. By clicking this button modal window has been opened, and after that I load form to this window by ajax.

I created php-file as handler for ajax requests. I need to get options from wordpress database, but I have no idea how to do it. if I try just include main file of plugin to the my handler I've got Internal server error 500...


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.

// to make AJAX action available for non-logged in users:

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


$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:

  1. define('SHORTINIT',true);
  2. Require wp-blog-header.php from WordPress root path.
  3. Include any needed WordPress includes manually.
  4. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.