Where to put my code: plugin or functions.php?

Question:

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):

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?

Answer:

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:

  • Modifying core WP filters (wp_head content, such as canonical links, generator and other HTML meta, etc
  • Site Favicon
  • Post-content shortcodes
  • Post sharing links
  • Google Analytics (and similar) footer scripts
  • SEO tools/controls
  • etc.

If 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 criterionwould 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:

  • Removing/overriding the WP core Gallery CSS
  • Filtering post excerpt length, “read more” text, etc.
  • Anything implemented via add_theme_support() (I suppose this one should be obvious)
  • Custom CSS

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.

Leave a Comment

Your email address will not be published. Required fields are marked *