Should “snippets” (short code or functions) be added to:
- the theme’s functions.php?
OR - make site-specific plugins?
Which is better from technical, security, performance, etc., standpoint?
Besides the reason that during an upgrade the theme’s functions.php may get overwritten, what would be the other reasons to make create site-specific plugins out of the “snippets?
From performance point of view, I don’t think there is any difference between functions.php and plugins : it’s just different places for the code to be.
I would be practical : if the function is part of the theme’s core (say, a scrollable slider in the header without which the theme is worthless) : it’s a function of the theme.
If it is a generic function that could be useful to another theme (say, custom data field), it’s a better choice to make it a plugin, because you could easily copy/paste this function if the files are separated from the theme.
The best option I would say is to create a child theme to the current theme and add all the snippets in the functions.php of the child theme. In this way the functions.php won’t get overwritten when the current theme is updated.
I’d go for a plugin. Posts containing shortcodes, for example, should not break when switching themes.
Be aware that you can also create must-use plugins. They work just like similar plugins but they can’t be disabled through the WordPress admin area.
For example, when creating a cooking site which depends on a custom post type “recipe”, I’d put stuff like registering the post type, custom taxonomies, shortcodes, new widgets, or custom admin meta boxes in a (must-use) plugin instead of the theme.
It seems like you are asking about adding your snippets to theme’s functions.php that is developed by someone else. Or put those snippets in the site specific plugin.
If you want to avoid your snippets and customizations of functions.php to be overridden by the theme update then you should use a child theme.
Now how can you decide to put the code in functions.php or in a site specific plugin. If the snippet is about adding functionality to theme, like registering thumnail sizes, or registering menu’s, or sidebars then it must be part of theme functions.php
If the snippet is about the site’s functionality then it must go into the site-specific plugin, for example registering custom post types, custom taxonomies or may custom permalink structure.
When I am not able to decide where to put the code by above criteria, I ask my self do I need this snippet when later I switch the theme if the answer is yes the snippet goes to the plugin and if the answer is no the snippet goes to the functions.php.