Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Should "snippets" (short code or functions) be added to:

  1. the theme's functions.php? OR
  2. 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?

share|improve this question
    
Thanks all for your responses. I use a child theme, so i am not concerned about losing snippets if I updated the theme or WP. My question is general to find out the best practice from technical, security, performance, etc., standpoint? –  Tara Nov 19 '11 at 20:05

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
Thanks good detailed reply. I use a child theme. Then does it even matter where I add the snippets? –  Tara Nov 19 '11 at 20:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.