Where Do I Put Code Snippets?

By all means you should avoid adding such snippets to the functions.php file of your theme or child theme. I know that this still recommended as the “default” location and you’ll find that hin in Facebook Groups, Forums, Tutorials etc. However, it is just the worst place to put a code snippet. It is not organized, it may be loaded globally even if not needed globally. And, the most important reason: you may lose the option to have your theme/ child theme updated automatically!

So, where to put those snippets then?

Alternative 1: In A Snippets Plugin

Well, there’s a plugin for that. And that’s a real good one called “Code Snippets”
This plugin is around for years, has matured a lot and is the go-to-solution for Code Snippets. Developers and Tutorial makers who care will recommend it. So do I.
It has a panic constant, basic error checking, you can organizes snippets, you can decide where to load the code (admin, frontend, global) and so on. It is really convienient.

An alternative Snippets plugin for CSS, JavaScript and HTML Snippts is “Simple Custom CSS and JS” – it is really awesome if you don’t need to add PHP code. They also have a pro version.

Alternative 2: In An “Extender” Plugin

There’s a company called “Cobalt Apps” which is specilizing in Tools for Site Builders, Themers and Developers for WordPress. They have lots of plugins out there to help you speed up your work. For Code Snippets you can use these plugins:

  • Extender Pro – works with these Frameworks/ Themes: Astra, GeneratePress, OceanWP, Genesis, Beaver Builder Theme, Freelancer, Twenty Seventeen, Twenty Sixteen
  • Genesis DevKit – works with Genesis Child Themes
  • Freelancer DevKit – works with Freelancer Framework

These plugins allow you to add custom CSS, custom JavaScript and custom PHP code to special areas and/ or special functions files. You are still able to update everything if you want.

Alternative 3: In A Functionality Plugin

If you are fine with organizing your code yourself, just try this alternative: collect the snippets in a plugin. You can activate, deactivate it at any time and also use it on as many sites as you want. Putting it on GitHub and in connection with the GitHub Updater plugin, this could be really powerful! It won’t touch your theme at all and you don’t lose any updates.

Bonus: For CSS just use “Simple CSS” 🙂

This plugin, Simple CSS, is easy to use, straightforward. You can work within the CSS Editor in WP-Admin Dashboard or in the Customizer – just as you like it. You always edit the same CSS. And, the big advantage over other “CSS” plugins, Simple CSS has in-post metabox with CSS additions for posts, pages or other post types. Very handy indeed.

An alternative here is the plugin “Simple Custom CSS and JS” as it can also be used for “CSS only” if you want that.