Skip to content

Instantly share code, notes, and snippets.

@jeffward3283
Created January 20, 2015 13:50
Show Gist options
  • Save jeffward3283/22a25dbf56d1e6ebc897 to your computer and use it in GitHub Desktop.
Save jeffward3283/22a25dbf56d1e6ebc897 to your computer and use it in GitHub Desktop.
<?php
////////////////////////////
# WP AJAX EXAMPLE
////////////////////////////
/////////////////////////
# Enqueue Script
/////////////////////////
add_action('wp_enqueue_scripts', 'ajax_items_enqueue_scripts');
function ajax_items_enqueue_scripts() {
# Setup Script
wp_register_script( 'ajax-items-script', admin_url('admin-ajax.php') . '?action=ajax_items_scripts', array('jquery'), '1.0', $footer = true );
wp_enqueue_script( 'ajax-items-script' );
# Localize script
$nonce_string = 'ajax-items';
$nonce = wp_create_nonce( $nonce_string );
$ajax_items_params = array(
'ajax_url' => admin_url('admin-ajax.php') . '?action=get_ajax_items',
'nonce' => $nonce,
);
wp_localize_script( 'ajax-items-script', 'ajax_items_params', $ajax_items_params );
}
///////////////////
# Ajax Results
///////////////////
add_action('wp_ajax_nopriv_'.'get_ajax_items', 'get_ajax_items');
add_action('wp_ajax_'.'get_ajax_items', 'get_ajax_items');
function get_ajax_items(){
///////////////////
# Verify nonce
///////////////////
$nonce_string = 'ajax-items';
$nonce = $_REQUEST['nonce'];
if( !wp_verify_nonce( $nonce, $nonce_string ) && !current_user_can('administrator') ) {
# Display the JSONP results
header("Content-Type: application/json");
$results = array('status' => 'error', 'message' => __('Invalid submit key.', TEXTDOMAIN));
echo json_encode($results);
exit();
}
# Display the JSONP results
header("Content-Type: application/json");
$results = array('status' => 'success', 'message' => __('You did it.', TEXTDOMAIN));
echo json_encode($results);
exit();
}
/////////////////////////
# JavaScript
/////////////////////////
add_action('wp_ajax_nopriv_'.'ajax_items_scripts', 'ajax_items_scripts');
add_action('wp_ajax_'.'ajax_items_scripts', 'ajax_items_scripts');
function ajax_items_scripts(){
# Begin Output button
ob_start();
# Display the output
?>
<script>
jQuery(document).ready(function($){
// Setup parameters
var xhr = '';
var _ajax_url = (typeof ajax_items_params != 'undefined') ? ajax_items_params.ajax_url : '';
var _nonce = (typeof ajax_items_params != 'undefined') ? ajax_items_params.nonce : '';
// Click to reveal the ajaxed items
$('#ajax-submit').on('click', function(e){
e.preventDefault();
// Abort previous request
if(xhr && xhr.readyState != 4) xhr.abort();
// Make the request
xhr = $.ajax({
url : _ajax_url,
type : 'POST',
data : {
'nonce' :_nonce
},
success:function(response, textStatus, jqXHR){
alert(response.message);
}, error: function(jqXHR, textStatus, errorThrown){
}
});
});
});
</script>
<?php
# Capture & clean the output
$output = ob_get_contents();
ob_end_clean();
$output = str_replace('<script>', '', $output);
$output = str_replace('</script>', '', $output);
# Output the results
header('Content-Type: text/javascript');
echo $output;
exit();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment