While creating the WPBeginner re-design, we saw a huge mistake that we made in our old site. All of our extra functionality that we added overtime was added in our old theme’s functions.php file. This not only caused our functions.php file to be super bloated, but it also increased the chance of error every time we modified that file. It was then we decided to take the advice of @otto42 and created a site-specific plugin. So you are probably wondering what a site-specific plugin is, why you need it, and how you can have it for your WordPress site. That is exactly what we plan to share in this article, so keep on reading.
What is a Site-Specific Plugin
Site-Specific WordPress plugin is a specific place to add snippets that you find on the web that performs functions that are not theme related. In other words:
Time to time again, you will read “how-to” articles related to WordPress all around the web (including our site) where we share snippets and suggest that you add them into your theme’s functions.php file. These snippets are not huge enough to be justified as plugins of their own, but some of them are certainly great to add specific functionality to a site that is not theme-dependent such as redirecting users to random post. Whereas other functions can be key to your specific site such as Custom Post Types.
This is when you need to add a site-specific plugin which essentially will serve as the same purpose of your theme’s functions.php file, but it will allow you to retain all these functionality when you are changing themes (see our checklist of things to do when changing WordPress themes).
Why You need a Site-Specfic Plugin for WordPress
As we mentioned above, it will allow you to retain all key additions to your site that you may have done overtime. Some functions that belong in a site specific plugin would be Custom Post Types, Adding Shortcodes, Adding WordPress Thumbnail Support, Redirecting users to a Random Post, and other independent functions that does not rely on your theme.
Above functions are something that you will need whether you change your theme or not. Can you imagine disabling a theme and then losing all of your shortocdes? That would be a total nightmare.
One of the reasons why we pasted beginner’s guide to pasting snippets from the web in WordPress was to prevent white screen of death that users get when pasting the snippet with wrong formatting. Often new users simply edit themes using the WordPress back-end to prevent the user of FTP. We have helped countless users who got locked out of their site when they pasted the wrong snippet.
When you use a site-specific plugin, you somewhat have a break-proof site. When you paste a snippet with wrong formatting, instead of the white screen of death the built-in plugin editor will recognize the error, and it will deactivate the plugin. Ofcourse this will break your site for a few seconds but at least you can go to the admin panel and rectify your mistake.
It is like choosing lesser of two-evils. Would you rather be locked out and have the site be broken. Or would you rather have the site broken but have the ability to fix it in seconds by simply undoing your mistake.
How to Create a Site-Specific WordPress Plugin
You may think that it may be super hard, but it really is not.
- All you have to do is create a new folder in the plugins directory. Example: /wp-content/plugins/yoursitename-plugin/
- Open a blank file and save it as “yoursitename-plugin.php”
- Put the following code in the file:
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
/* Start Adding Functions Below this Line */
/* Stop Adding Functions Below this Line */
- Now upload this file into the folder you created in the plugins directory. Then simply activate the plugin.
From this point on, we will recommend adding the appropriate snippets we share into site-specific plugins rather than theme’s functions.php file.
We hope this will start a new trend that others will follow. We have already joined the band-wagon, and we recommend that you do this as well. It is a best-practice that will save you time in the long-run.
Article from http://www.wpbeginner.com/