Is there any reason to use admin-ajax.php for ajax requests versus a custom page template?
I didn't know about admin-ajax.php until recently, so what I had been doing is creating a custom page template like this:
<?php
/**
* Template Name: API
*/
if ( isset( $_GET['ajax_request'] ) ) {
// do stuff
}
And the ajax call would be to the URL http://mysite.com/api/, which is where I've published a blank page using my API page template. This seems to give me access to all my WordPress functions and spit out data.
However, recently I've read up on admin-ajax.php and understand another way to connect to the WordPress database is to call the URL http://mysite.com/wp-admin/admin-ajax.php and have functions like this:
add_action( 'wp_ajax_nopriv_action', 'my_do_stuff' );
add_action( 'wp_ajax_show_action', 'my_do_stuff' );
function my_do_stuff() {
// do stuff
}
Is it wrong to connect the first way? Does admin-ajax.php provide extra security or something? Thanks any input!
http://site.com/api/
maps tohttp://site.com/wp-admin/admin-ajax.php