Site-Specific WordPress Plugin for Code Snippets

Last updated on October 18th, 2014 by Robert Abela. Filed under WordPress Security Tutorials

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:

  1. What is a Site-Specific Code Snippets WordPress Plugin
  2. Why you need a Site-Specific Code Snippets WordPress Plugin and what are the advantages
  3. 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.

Improve Reliability

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

  1. Create a new directory in the WordPress plugin directory (/wp-content/plugins) and name it after your site, for example /wp-content/plugins/wpwhitesecurity/
  2. 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
  3. 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.

WordPress Hosting, Firewall and Backup

WP White Security is hosted on A2 Hosting, protected with BBQ:Block Bad Queries Firewall and backed up with BlogVault online WordPress backup service

2 comments

Paal Joachim 12/08/2014

I have over the last month or so been gathering code snippet and placed them into a custom functions plugin. I am still working on it, and will sometime soon create a settings screen for the various options I have placed into it.

Do you guys have a list of these gathered into a blog post?
If not could you share a few of them related to security?

Robert Abela 12/08/2014

HI Paal,

Thank you for following our blog.

If I am not mistaken we do not have any specific code snippets related to security. We have several guides, htaccess and wp-config.php code snippets, but not specific PHP code snippets that could be used in site-specific plugins.

Leave a Reply

Your email address will not be published. Required fields are marked *