My code snippets are presented as small, single file plugins that are easy to use - you just upload and activate.
When a post contains code snippets people often ask where to put the code. Many times the reader is directed to copy the code into their theme’s functions.php file (less the opening <?php line). I do things differently.
Why I dislike editing functions.php
I dislike the use of functions.php for a number of reasons.
- If the site owner makes any mistake copying the code into functions.php it is likely to break their site. If they are editing the file via the Appearance/Editor page in the WordPress admin area (a bad idea even at the best of times) they may not be able to get back into that page to fix the error. (WordPress 4.9 introduced PHP error checking so this is a little less likely now.)
- If the site owner wishes to disable the functionality it requires editing functions.php file again, with similar risks.
- If the site owner changes their theme then the functionality is lost until they copy the changes to the new theme’s functions.php file.
Easy to manage
My code is generally theme independent so it is still relevant when the theme is changed. Each snippet is presented as a single file plugin that can be activated and deactivated as necessary. In the unlikely event of a compilation error in the code, WordPress will not activate it and will report the error.
Step by Step
- Go to the post in question. In this demo I will be using the Customise “Choose an option” dropdown item post.
- Scroll down to the code snippet and click view raw to see only the code in question.
- Save this code-only page to your computer.
- Upload it to your site.
- Activate the plugin.
In this video of the process I save to a local environment to speed things up.