Is there an easy to understand scheme to decide what kind of code belongs to a plugin or the themeâs functions.php
?
There are many cases and many debates about that topic, mostly because there are some misconceptions about the inner workings of WordPress. I am asking for an answer based on facts, not on opinions.
It should explain how to handle these points (and probably more):
- custom post types and taxonomies
- contact forms
- shortcodes
- custom widgets
add_theme_support( 'automatic-feed-links' );
- SEO functions like custom
meta
elements - theme switch
There are often pros and cons for both sides. Our most popular question Best Collection of Code for your functions.php file got a lot of code snippets as answers that are at least debatable.
We need criteria a beginner can understand, maybe a check list â with reasons.
See also the related question by Chip Bennett on our meta site: Questions specifically asking for a solution “without a plugin”
Related: Where do I put the code snippets I found here or somewhere else on the web?
I would start with this question: Is the functionality related to presentation of content, or with generation/management of content, or of the site, or of the user identity?
If the functionality is not related specifically to presentation of content, then it is squarely within Plugin Territory. This list is long:
wp_head
content, such as canonical links, generator and other HTML meta, etcIf the functionality is related to presentation of content, then it is a candidate for being included in the Theme. At this point, I would revert to @Raf912’s Theme-switch criterion: would you miss the functionality when you switch Themes? If the answer to that question is no, then the functionality belongs in the Theme. Some examples:
add_theme_support()
(I suppose this one should be obvious)Normally, these two questions will provide a fairly clear line of differentiation; however, there are exceptions.
Custom Post Types
Custom Post Types, for example, are a bit of a unique hybrid of content generation and presentation, given the way the Template Hierarchy works for single-post-type archive index pages and single post pages. The content-generation aspect of CPTs would normally place them squarely in Plugin Territory; however, Plugins cannot define template pages that inherently fit into the design/layout/style for any given Theme (especially if the CPT displays other than the usual Title/Content/Meta, or has custom taxonomies associated with it).
Long-term, the solution to this disparity, IMHO, is to have a standard convention/consensus for the definition of CPTs for given types of content (real estate listings, calendar events, e-commerce products, book/media library entries, etc.). That way, user-generated content would remain portable between Themes that implement the standard/convention definition of a given CPT, while Theme developers retain the flexibility to define the design/layout/style of that CPT in the Theme template files.
Social Media Links
Similarly, I would normally say that social media profile links, ave become all but ubiquitous in current Themes, are Plugin Territory, because they have nothing to do with presentation of content. The best solution would be for these profiles to be defined somewhere in core; however, there is currently no standard/consensus means of defining these links. Are they best-defined at the site-setting level, or on a per-user basis? If per-user, which user’s meta gets exposed in the template? etc.
So again, long-term, the solution to this disparity is for either core to define where these links are defined, or else for the Theme developer community to develop its own consensus. In the meantime, there’s really nothing for it but to keep them defined within each Theme.
An easy test where the code is best placed:
do you miss the functionality, is the blog not proper working or fragments of the old theme (e.g. shortcodes) are left?
yes: put it into a plugin
no: leave it in functions.php
Examples: Write a shortcode. After switching the theme, the plain shortcodes are left in your posts. So it will be better placed in a plugin.
Write a function to list the last comments. After switching the theme, everything is ok because maybe the other theme have an equivalent function.
It really depends on the code and what it will do. Some code only influence the styling or content of the theme, some others will modifiy blog posts.
I don’t think there’s an easy answer to this question, but I bet we could make a flow chart to help with the decision. Here’s a rough outline of such a flow chart, which can and should be expanded. Comment with suggestions!
Some other thoughts that I didn’t know how to fit in here:
From here Themes VS Plugins
Add custom code to a child theme so when you update the parent theme, your custom code is not lost.
You can also create a site specific plugin which contains all your custom code as well.
As far as writing code versus plugins, you can use plugins to and the functions however for most of what you want, hand coding is the best as its easier to modify except in some cases like meta boxes where you may consider using a plugin unless you’re a theme developer.
http://codex.wordpress.org/Plugin_API/Filter_Reference/user_contactmethods
I know this is a dead horse and that Chip has pretty much covered it, but wanted to add a few thoughts.
If you make a living programming and find yourself working on wordpress sites under deadlines, you are going to find that it really comes down to time.
More often than not, especially for those just starting out, it is much faster and simpler to just add whatever you need into a theme and call it done.
That being said, if you work on wordpress on a semi regular basis, you should seriously consider doing the following:
This should handle everything that you will commonly need to do with a plugin, including activation, deactivation, version updating, building admin panels, and uninstalling.
If you make the time to do this, you will find:
You can now build things out properly and get future projects done more quickly.
This should handle everything that is commonly needed in a theme:
Once you have that done, build out a child theme skeleton that uses your primary theme.
Once you have these two things done, creating new sites for people becomes much faster.
If you do the above, you can then work on the following:
And, if you do all of the above, you will find that Chip’s answer will then not only be ideal, it will become optimal.
The simple answer is this..
Is the code dependent on any of the functionality built into a specific theme? If yes, then put in a theme.
Do you want this code to be transferable between sites and between themes? If yes, then put in a plugin.
If the answer is no to both of the above, then picture the site 5 years in the future, when it’s time for a redesign. Is the function of the code you’re writing something that will survive the next design update? If yes, put in a plugin.
Also, if you’re not using child themes and you plan to update the theme, I would also suggest you use a plugin.
To answer OP:
custom post types and taxonomies
Should go in a plugin as they define stuff on a structural/conceptional level, not a presentation level
contact forms
Are presentational should go in theme functions
shortcodes
In plugin as structural
custom widgets
Are very much presentation and should go with the theme: functions
add_theme_support( ‘automatic-feed-links’ );
Theme support goes in theme functions
SEO functions like custom meta elements
Presentation, thus in functions