What are the Best Practises When Using AJAX in Plugin Development?

WordPress AJAX best practices are following:

  1. Use wp_localize_script to declare JavaScript global variables;
  2. Use admin-ajax.php script to handle all AJAX requests;
  3. Use nonces and check for permissions;
  4. Use jQuery Form plugin to submit forms.

Read 5 tips for using AJAX in WordPress article for more details.

What do you do when the user activates your plugin, do you need to
create a function to write your code to functions.php on activation?
Similarly, what if the user changes theme?

You don’t have to care about it. If you write your own plugin and it uses AJAX requests, then it has to be implemented only inside your plugin, don’t write code into theme’s functions.php file.

error code: 523