WordPress – Plugins or Theme Functions.php

__UPDATE__: I’ve taken a second stab at the topic on [WP Theme Tutorial][wptheme].

While I’ve mentioned it in my presentations on [Mobile WordPress development](http://www.slideshare.net/curtismchale/mobilizing-wordpress) I’ve never really expanded on my thoughts on building WordPress features in the theme functions.php file or in a plugin.

### What’s the Difference?

If you’re just starting out in WordPress you may not even know what the difference is between the two options. The really really short version is that items in your functions.php only run in the theme an plugins run all the time. Yes most of the time plugins can be dropped in to a theme’s functions.php and will run fine and visaversa.

### What does functions.php Do?

The [functions.php](http://codex.wordpress.org/Theme_Development#Functions_File) file is pretty standard in WordPress themes now. It’s certainly not required but it’s not very often you won’t find one in a theme. Code put in your functions file runs in the theme and that’s about it. You’ll find stuff like [register_nav_menu()](http://codex.wordpress.org/Template_Tags/register_nav_menu) and [register_sidebar()](http://codex.wordpress.org/Function_Reference/register_sidebar) among many other things both of which are specific to the theme and may change with a site redesign.

### What about a Plugin?

Plugins are typically found on the [WordPress.org Plugin Repository](http://wordpress.org/extend/plugins/). They can do everything you can from a theme’s functions.php file but don’t only run with the theme, they run all the time. Plugins need to be installed and activated from the Plugins menu. If they’re not activated then they don’t run (with the exception of mu-plugins for multisite). Often plugins have much more functionality than simply declaring a sidebar or menu, though they certainly can be just that simple.

### Why to Use a Plugin

Now that we’ve got some context on what a plugin is lets look at a really good example of why we want to do it. We all know that mobile devices are everywhere. Most of us have iPhones, iPads or Android devices so it’s pretty often that a mobile version of a site needs to be developed. If you’re using [WP Touch Pro](http://www.bravenewcode.com/store/plugins/wptouch-pro/?wptouch_affiliate_id=3519&utm_source=affiliate-3519&utm_medium=affiliates&utm_campaign=text1) (the best option in my opinion) it won’t run all but the most basic of items in your functions file. Specifically items abstracted with TEMPLATEPATH will not get run since WP Touch Pro intercepts the request before WordPress has established that constant.

So if you’ve put your custom post type or taxonomy in the theme WP Touch Pro won’t know about it and you won’t be able to run queries against it. If you’ve built it as a plugin you’re good to go, WP Touch Pro won’t have any issues with it.

Custom Post Types are another great example of why we want to use plugins. Say we’re building a Classifieds site and have a CPT call Classifieds with some appropriate custom taxonomies. If we bundle the CPT and CT in to the theme then when we change the design we’ve got to remember to move over those chunks of code or we’ll loose them (well they’ll still be in the DB just not easily accessible). If we add them using a plugin then we’re good to go when building a new theme since they’re not affected by the presence of a particular theme.

### Tough Cases

Yeah there are some tough cases. If you’re loading jQuery from the Google CDN you may want it to live on to the next site design or you may not. If you’re building a featured content slider you may want it in the next theme or you may not. It’s not always cut and dry so you’ve got to use your best judgement.

### Summary

The real basic summary is that if the feature is purely for this specific site design then it’s a theme function. If it’s something you probably want to live on to through redesigns of the site then it should be put in a plugin.

[wptheme]: http://wpthemetutorial.curtisdev.ca/2012/03/20/where-does-my-code-go-functions-php-or-a-plugin/ “Where Does My Code Go functions.php or a Plugin?”