Site-Specific Plugins: A Better Alternative to the WordPress functions.php File

Following best practices usually makes a developer’s life much easier in the long run, but the mindset often becomes less and less appealing as a deadline, or happy hour, draws near.  In other cases, best practices arrive conveniently on the scene… after one has laboriously done something a different way.

Both easy to access and quick to edit, the functions.php file of a WordPress installation has been the focal point of countless tutorials and how-tos, which guide users through adding custom features and functionalities to their blogs.  Though editing this file is no doubt a legitimate and commonly used way to add custom functions, it is subsequently, a far-from-best-practice solution for multiple reasons:

  • The customizations are theme-specific, not site-specific
  • Syntax errors in the customizations can break a site
  • Custom code crammed into the file can be difficult to keep organized

For these reasons, more and more developers are avoiding extra work by ceasing to edit the functions.php file and creating a site-specific plugin to store their custom functions.

What is a site-specific plugin?
Think of a site-specific plugin as a file made specifically for a developer to add custom functions to.

  • A site-specific plugin is used the exact same way one would use a functions.php file to add customizations
  • A site-specific plugin contains no code except a developer’s custom functions
  • A site-specific plugin adds functions to an entire site, and not just to a theme

So why is creating a site-specific plugin to store custom functions a better solution than storing them in a functions.php file?

  • More flexibility and seamlessness
  • More safety and reliability
  • More organization and efficiency overall

1. More flexibility and seamlessness
Adding custom functions to a WordPress blog by editing the functions.php file will, almost certainly, cause unnecessary work down the road.  Since the file lies in a WordPress blog’s theme directory, customizations made there are theme-specific, a fact that is often overlooked until an upgrade or change in themes is attempted.  In the simplest terms, this means that any customizations a developer has added to a blog by editing the functions.php file will not be present when the new theme is activated.

When utilizing a site-specific plugin, switching between themes becomes a seamless process with all relevant customizations intact; no additional work is necessary.  Any change in theme can be tested and applied with ease since the developer doesn’t have to worry about copying and pasting code from an edited functions.php file, or creating a duplicate, to replace the new theme’s existing one.

Using a site-specific plugin also allows a WordPress blog’s other active plugins to access its custom functions.  It eliminates the need for a developer to make any extra customizations to ensure newly added plugins integrate with the site-specific functions as intended.  It makes working with mobile themes, such as those supported by WPtouch Pro, especially easy for developers.

2. Safety and Reliability
Since the functions.php file contains a blog’s default features and functionalities, tinkering with it can be dangerous.  When editing the file, any error in syntax can lead to what many developers refer to as “the white screen of death,” which breaks the site, and locks the developer out of the admin area.

Site-specific plugins create a much more user-friendly environment for adding custom functions, since by default, the plugin is deactivated right away if it causes a breakage.  In other words, if something goes wrong, the plugin shuts down, not the entire site.

3. Organization and Efficiency
Instead of packing the functions.php file to the brim, making it extremely large and extremely difficult to navigate, a site-specific plugin provides developers with a single area to store all their custom functions, separate from where the blog’s default ones are stored.

Site-specific plugins allow developers to be more efficient, eliminating both the need to recreate customization efforts, and having to search through an edited functions.php file to find them.

The Process
Creating a site-specific plugin takes less than five minutes.

Simply create a file in the directory /wp-content/plugins/, name it, and add the code below:

<?php
/*
Plugin Name: My Site-Specific Plugin (or whatever you like)
Description: All site specific functionality goes here.
*/
/* Add your functions below this line */
/* Add your functions above this line */
?>

When editing the functions.php file is okay
There are definitely specific instances when editing a functions.php file is the best way to go, but only when a developer’s intention is to create custom functions that apply directly to a specific theme.  A good question to ask when making a judgment call between which method to use is, “Upon changing themes, should this functionality or feature stay active on the site?”  If the answer is yes, the function should be stored in a site-specific plugin, not the functions.php file.

- This article was written by BraveNewCode community member and guest contributor, Jeff Chenoweth. Connect with Jeff on Twitter at @mrjeffchenoweth

Are you interested in writing a guest post for our site?  If so, contact us via our contact form.