Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am in the middle of creating a plugin and everything works fine with the AJAX calls but one question I can't seem to find the answer to is about best practise.

I route the AJAX calls through /wp-admin/admin-ajax.php and have my php functions in the theme functions.php script.

However, I've just thought that this doesn't seem such a good idea because 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?

Therefore, I was just wondering if there are other ways to do it or what is the common approach for this type of thing.

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks, I didn't know you could place the functions in your plugin file rather than the theme functions.php, I always thought that they just had to be in the theme file because that's where admin-ajax.php 'looked' for the functions. –  martincarlin87 Mar 6 '13 at 9:41
    
@martincarlin87 Yes, you can write your AJAX hook functions in your plugin file too. –  Eugene Manuilov Mar 6 '13 at 9:43
    
makes sense now, thanks Eugene –  martincarlin87 Mar 6 '13 at 9:47
    
Also hint for a digital book about the topic: wpajax.com –  bueltge Mar 6 '13 at 10:32
add comment

Check the documentation on the Codex about AJAX in Plugins.

Since Ajax is already built into the core WordPress administration screens, adding more administration-side Ajax functionality to your plugin is fairly straightforward.

What you have to do is, instead of putting add_action('wp_ajax_my_action', 'my_action_callback'); in your functions.php, you will put them in your plugin file.

share|improve this answer
    
I see, I didn't know you could place the functions in your plugin file rather than the theme functions.php, I always thought that they just had to be in the theme file because that's where admin-ajax.php 'looked' for the functions. –  martincarlin87 Mar 6 '13 at 9:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.