Skip to content

Instantly share code, notes, and snippets.

@agustibr
Created October 21, 2011 23:49
Show Gist options
  • Save agustibr/1305291 to your computer and use it in GitHub Desktop.
Save agustibr/1305291 to your computer and use it in GitHub Desktop.
Frontend AJAX in WordPress
<?php
//Frontend AJAX in WordPress //http://codex.wordpress.org/AJAX_in_Plugins
define('DOING_AJAX', true);
/* Assuming you will put this file in your plugin's directory */
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'wp-load.php');
if(!isset($_REQUEST['action']) || trim($_REQUEST['action'])=='')
{
die('-1');
}
@header('Content-Type: text/html; charset='.get_option('blog_charset'));
/* Including your plugin's main file where ajax actions are defined */
include_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'main-plugin-file.php');
send_nosniff_header();
if(has_action('wp_ajax_'.$_REQUEST['action']))
{
do_action('wp_ajax_'.$_REQUEST['action']);
exit;
}
status_header(404);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment