Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created October 27, 2011 20:57
Show Gist options
  • Save andredublin/1320847 to your computer and use it in GitHub Desktop.
Save andredublin/1320847 to your computer and use it in GitHub Desktop.
Usually I have to create custom scripts to respond to ajax
/**
* Define WP_USE_THEMES
*
**/
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');
header('HTTP/1.1 200 OK');
header('Content-type: application/json;');
@andredublin
Copy link
Author

When creating custom php scripts that will respond to ajax calls it is necessary to include core wordpress files and set the response headers to 200 and json. Make sure to have a condition that prevents users from viewing the file directly such as a preg_match for your $_POST or $_GET constants index or if the required index of either super global array is not set then initiate die() or exit() or redirect with header()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment