Hate to switch themes because you lose all the custom snippets you added to the theme’s functions.php file? Then today’s tip is for you!
I can’t take credit for this one, as I found it browsing WPBeginner. But it is definitely a solution that needs sharing over and over until it becomes the new standard.
Why the Urgency
Well, as I’ve already mentioned, the standard has been to add any snippets needed by your site to do certain tasks to your functions.php file. This was definitely great advice (compared to directly modifying core files – which is a big no-no).
However, it has led to:
- Highly bloated functions.php files,
- Possibly wrecking your theme with any edits done to that file,
- And the biggie for me – Having to re-add all my snippets every time I switch themes.
Everyone should know about this wonderful, stress reducing tip!
How to Create a Site-Specific WordPress Plugin
You can create a site-specific WordPress plugin in 4 simple steps:
1. Create a new folder in your plugins directory. For example:
2. Create a new blank file and save it in that folder as “sitename-plugin.php”.
3. Add the following code to your new file:PHP
12345678 <?php/*Plugin Name: Site Plugin for example.comDescription: Site specific code changes for example.com*//* Replace this whole line with your site-specific WordPress plugin code and snippets. */?>
4. Now activate the plugin in your WordPress dashboard.
And you’re all set!
This suggestion has saved me a ton of time already. It also makes me much more confident when writing new themes for my clients that I won’t ruin any existing functionality I wrote before.
I hope you consider making the switch from editing your theme’s functions.php file to writing a site-specific plugin, too.