Created
August 26, 2014 14:37
-
-
Save jlittlejohn/b6695971d706732a1165 to your computer and use it in GitHub Desktop.
WP: Enable Ajax in WordPress
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 | |
| // 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