Skip to content

Instantly share code, notes, and snippets.

@Vinsanity
Created August 20, 2014 04:21
Show Gist options
  • Save Vinsanity/46f18c55b258cf74163d to your computer and use it in GitHub Desktop.
Save Vinsanity/46f18c55b258cf74163d to your computer and use it in GitHub Desktop.
WordPress Front-End Ajax Loader
<?php
function 3five_wpajax_url_script() {
$output = '<script type="text/javascript">';
$output .= 'var ajaxurl = '. admin_url('admin-ajax.php');
$output .= '</script>';
echo $output;
}
// Enqueue and localize the ajax script for the staff page.
function 3five_ajax_load_scripts() {
wp_enqueue_script('work-ajax', get_template_directory_uri(__FILE__) . '/js/work-ajax.min.js', array('jquery'));
wp_localize_script('work-ajax', 'work_vars', array(
'3five_nonce' => wp_create_nonce('3five-nonce')
)
);
3five_wpajax_url_script(); // Loads the admin ajax function.
}
add_action('wp_enqueue_scripts', '3five_ajax_load_scripts');
if ( !function_exists( '3five_ajax' ) ) {
function( '3five_ajax' ) {
if( !isset( $_POST['3five_nonce'] ) || !wp_verify_nonce($_POST['3five_nonce'], '3five-nonce') )
die('Access Denied');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment