Using AJAX in WordPress is fundamentally pretty simple.
You need a function that handles the AJAX request, and a couple of add_action
calls to map that function to the action
you pass to admin-ajax.php
, in this case, some_action
:
function wpd_ajax_function() {
get_template_part( 'my-content' );
wp_die();
}
add_action( 'wp_ajax_some_action', 'wpd_ajax_function' );
add_action( 'wp_ajax_nopriv_some_action', 'wpd_ajax_function' );
This would go in your theme’s functions.php
file. You can see that all the function does is load your my-content.php
file, and then exits execution.
Once you have that in place, you can test this by visiting admin-ajax.php
in your browser with your some_action
action appended:
http://yourdomain.com/wp-admin/admin-ajax.php?action=some_action
and you should see your output. If you hit that URL with your jQuery.load()
code, you should have everything you need.