Skip to content

Instantly share code, notes, and snippets.

@r-a-y
Created March 20, 2013 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save r-a-y/5209075 to your computer and use it in GitHub Desktop.
Save r-a-y/5209075 to your computer and use it in GitHub Desktop.
Lightweight, frontend version of admin-ajax.php for WordPress.
<?php
/**
* Lightweight, frontend version of admin-ajax.php.
*
* Only loads wp-load.php and sets up the headers and AJAX actions.
*/
// Setup 'DOING_AJAX' constant to be compatible with native WP functionality
define( 'DOING_AJAX', true );
// Load WordPress Bootstrap
// Adjust this depending on where you're including this from
require_once( '../../../wp-load.php' );
// Allow for cross-domain requests (from the frontend)
send_origin_headers();
// Require an action parameter
if ( empty( $_REQUEST['action'] ) )
die( '0' );
// Setup headers
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
@header( 'X-Robots-Tag: noindex' );
send_nosniff_header();
nocache_headers();
// Setup ajax action hooks
//
// Authenticated actions
if ( is_user_logged_in() ) {
do_action( 'wp_ajax_' . $_REQUEST['action'] );
// Non-admin actions
} else {
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
// Default status
die( '0' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment