Functions.php Vs Writing a Plugin

Development

When it comes to developing a new site, every developer has a stash of favorite code snippets or reusable modules they keep handy.  It could be a homemade widget, a section or module that is adaptable to multiple sites, or custom code that you use to personally extend the functionality of WordPress, but every developer worth their salt has them.  But over and over again I keep hearing the same question from new and old developers alike: Should these snippets be put into the functions.php file, or stored in a plugin?

What’s The Difference?

If you’re not familiar with it, the functions.php file is a special template file that stores any theme specific code that runs only when your theme (or a child theme) is activated.  Most times, pro themes will put all of their “on theme activation” codes, option panels, color pickers, and what-not in the functions.php file – or at least, in a file that’s included IN the functions.php file.

Plugins, on the other hand, will run no matter what theme you have.  You could change themes once a day for 30 days and still have the functionality of your plugin going strong.

Best Practices

The way I see it, there’s a fairly standard rule of thumb to use when it comes to this sort of thing: if you have a bit of functionality that needs to run on any theme, then it should go in a plugin.  If your functionality is theme specific and won’t transfer over to another theme, it should probably go into the theme’s functions.php file.

A Few Examples

Goes In A Plugin

  • Custom Post Type Setup
  • Custom Taxonomies
  • Google Analytics Code (if you add it via a hook to wp_footer() )
  • oEmbed extensions (Twitch, JustinTV, other services)

Goes in the Theme File

  • Custom Image Sizes
  • Javascript / Other Script Enqueues

The differences between these lists: The theme functions would stop working if the theme was switched, but  you’d still have Google Analytics, all your custom post types / taxonomies, and any extensions.

Agree? Disagree?  Have any special practices or rules of thumb you use for your own development?  Leave them in the comments below!