Created
November 12, 2012 23:08
-
-
Save melchoy/4062681 to your computer and use it in GitHub Desktop.
WordPress get post data via ajax with json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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