I’m wondering what the preferred method is for dealing with AJAX calls. Should one use the same plugin php file to process the POST or a separate one? Which is cleaner or safer?
Leave a Reply
You must be logged in to post a comment.
I’m wondering what the preferred method is for dealing with AJAX calls. Should one use the same plugin php file to process the POST or a separate one? Which is cleaner or safer?
You must be logged in to post a comment.
the “safer and cleaner” way would be to use admin-ajax.php that comes with wordpress and
wp_ajax
hook to call your processing function from your plugin file and use wp-nonce to check the integrity of the call.for example:
your ajax JQuery call would be
then in your plugin file add
*if you want logged in users and guests to access your function by ajax then add both hooks.
*ACTION_NAME must match the action value in your ajax POST.
then in your function just make sure the request came from valid source
Hope this Helps