Many guides, tutorials, forum posts, or other resources that have any code snippet say something like “Add it to your theme’s functions.php
”. Some may mention that you can create custom plugin just for this. But I didn’t see another, better way that you should use: Must-Use (MU) plugins.
Must-Use plugins are a little different than regular plugins. There are many articles where you can find how they are different and what is their advantage but here is a quick summary:
- They are located in directory
/wp-content/mu-plugins
while regular plugins are located in/wp-content/plugins
. - Every
.php
file in that directory (top level, like/wp-content/mu-plugins/file.php
, not in subdirectory like/wp-content/mu-plugins/sub/file.php
) is automatically loaded on each page load (alphabetically), without need for activation or deactivation. Each file is loaded until you delete it. (You can have files is subdirectories, you just need to manually include them in top-level files.) - They are never rewritten during update.
- They persist when you change themes.
- They are loaded before regular plugins or themes so you can add hooks as early as possible.
Any code snippet you find online or any other custom code you have can be placed here. You can have file for anything you find important, named so that you can easily find, edit, or delete, safe that it won’t dissappear with theme or plugin update.