Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Created August 26, 2014 14:37
Show Gist options
  • Save jlittlejohn/b6695971d706732a1165 to your computer and use it in GitHub Desktop.
Save jlittlejohn/b6695971d706732a1165 to your computer and use it in GitHub Desktop.
WP: Enable Ajax in WordPress
<?php
// Put JavaScript into Template
function enable_ajax_functionality() {
wp_localize_script( 'ajaxify', 'ajaxify_function', array('ajaxurl' => admin_url('admin-ajax.php')) );
}
add_action('template_redirect', 'enable_ajax_functionality');
// Add two actions to wp_ajax, where we get posts array back as json
function test_ajax() {
header( "Content-Type: application/json");
$posts_array = get_posts();
echo json_encode($posts_array);
die();
}
add_action("wp_ajax_nopriv_test_ajax", "test_ajax");
add_action("wp_ajax_test_ajax", "test_ajax");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment