When authors suggest a code snippet (piece of code) in their WordPress tutorials to introduce new functionality in WordPress, WordPress administrators add such code snippet to the theme’s functions.php file.
There are a number of limitations and disadvantages when you add code snippets to the theme’s fucntions.php file, for example when you change the theme you have to migrate most of the code. Instead it is recommended to create a Site-Specific Snippets WordPress plugin, which is used specifically for these code snippets. In this blog post we will explain:
- What is a Site-Specific Code Snippets WordPress Plugin
- Why you need a Site-Specific Code Snippets WordPress Plugin and what are the advantages
- 3 Easy Steps to Create a Site-Specific Code Snippets WordPress Plugin
What is a Site-Specific Code Snippets WordPress Plugin
If you would like to add new functionality specific to your WordPress blog or website, such as configuring a number of pages to run on SSL, or something you’ve read about in our WordPress security tutorials and hacks, you can add these code snippets directly to the site-specific code snippets plugin file rather than to the theme’s functions.php file.
Why your WordPress needs a Site-Specific Code Snippets Plugin
More Flexibility and Better Integration
Developers frequently release theme upgrades, especially when using premium themes. Each time you upgrade your theme the custom code snippets you added to your functions.php file will be overwritten, unless you manually upgrade the theme, or manually copy the code snippets from one functions.php file to the other.
If the code snippets you find in WordPress tutorial is added to the Site-Specific Code Snippets WordPress plugin instead, you do not have to worry about upgrading the theme or forgetting to merge the code. Therefore theme upgrades can be done seamlessly.
By using a site-specific Code Snippets plugin you can also allow other active WordPress plugins installed on your blog or website to be able to access the custom functions, hence avoiding extra customizations.
We always recommend everyone to test website customizations and new code snippets on a test website before putting them on the live website.
When you add new code snippets to your WordPress theme’s functions.php file, there are risks that the code might break your website and you get a WordPress Whitescreen of death.
On the other hand, should a WordPress php code snippet be problematic when added to the plugin, the Site-Specific plugin will be automatically disabled rather than breaking down your website, hence it increases reliability.
How to Create a Site-Specific WordPress Plugin
- Create a new directory in the WordPress plugin directory (/wp-content/plugins) and name it after your site, for example /wp-content/plugins/wpwhitesecurity/
- Create a new php file in the new directory. The name of the file could be anything you want, but just to keep things standard, you can name it as your site, e.g. wpwhitesecurity.php
- Add the below code snippet to the php file:
<?php /* Plugin Name: Site-specific plugin for wpwhitesecurity.com Description: site-specific plugin code in this file */
Once you added the code snippets, save the file and enable the site-specific plugin from the WordPress dashboard.
No need to worry again about missing some code snippet, forgetting to migrate it, or breaking some functionality when upgrading your theme or your WordPress. Next time you encounter a WordPress code snippet and would like to add it to your website, add it to the site-specific plugin file.