Skip to content

Instantly share code, notes, and snippets.

@melchoy
Created November 12, 2012 23:08
Show Gist options
  • Select an option

  • Save melchoy/4062681 to your computer and use it in GitHub Desktop.

Select an option

Save melchoy/4062681 to your computer and use it in GitHub Desktop.
WordPress get post data via ajax with json
<?php
// Make ajax requests to /wp-admin/admin-ajax.php?action=get_post&id=127
add_action('wp_ajax_get_post', 'figgis_get_post_json');
add_action('wp_ajax_nopriv_get_post', 'figgis_get_post_json');
function get_post_json() {
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
header('Content-type: application/json');
$post_id = htmlspecialchars($_GET["id"]);
$post = get_post($post_id);
$title = $post->post_title;
echo json_encode($post);
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment