WordPress Quick Tip: Functionality Plugins

  • follow us in feedly
Published August 15, 2013 by Brad Knutson
WordPress Quick Tip: Functionality Plugins

Every WordPress Developer is guilty of writing functions and then placing them in their WordPress theme’s functions.php file. If you’ve read any of my blog posts, you are well aware I do it all the time! There isn’t necessarily anything wrong with it, but there is a better practice.

To keep your WordPress installation as modular as possible, we need to rethink things a little bit. We first need to determine if the function that we planned on dropping into the functions.php file is a theme-specific improvement, or something that we would want to use regardless of the theme selected.

It’s not all that difficult to figure out if you think about it. If it’s a change to the WordPress admin, or a style-agnostic function, then it should be built into it’s own plugin. We call these plugins “Functionality Plugins.” If it’s theme specific and dependent on styles from external stylehsheets, then it belongs in the theme’s functions.php file.

By building Functionality Plugins, we’re able to use our precious functions even if we change themes, or port them to different installations of WordPress. It also makes it much easier to debug if your code is causing errors. All you have to do is disable the plugin and your site goes back to normal!

Let’s look at how we can create our own Functionality Plugin with a really simple step-by-step example. Feel free to download the source ahead of time.

Download Source

1. Create a plugin folder

In your local file system, create a folder in your WordPress plugins folder (/wp-content/plugins/). Name this folder appropriately so you’ll know what it is later. For this example, I chose functionality-plugin-test.

2. Create a .php file inside your plugin folder

Again, the name doesn’t matter, just name it appropriately so you’ll recognize the file if you need to go back and debug later. For this example, I chose the name test.php to prove that the file doesn’t need to be named index.

3. Add plugin info

Open up your newly created plugin file in your code editor of choice. Add something similar to the code below to the top of your file.

<?php
/**
 *
 * Plugin Name: Functionality Plugin Test
 *
 * Description: Test functionality plugin.
 *
 * Version: 1.0
 *
 * License: GPL
 *
 * Author: Brad Knutson
 *
 * Author URI: http://bradsknutson.com
**/

Name your plugin appropriately, and fill in the description, your name and URL.

4. Add the code to your file

Add the code that you normally would have added to your functions.php to your plugin file after your initial comments. This could be a shortcode, or some other functionality that you want. Here is a basic example using a simple shortcode.

function test_functionality_plugin_shortcode() {
	$content = 'It works!';
	return $content;
}
add_shortcode('functionality', 'test_functionality_plugin_shortcode');
?>

All this function does is create the shortcode [functionality] which prints "It works!" on the screen in it's place.

Remember to close your file out with a closing PHP tag.

FTP your files to WordPress, and activate in the Admin

We're done creating folders and files. Now, you can either import a zipped file into the admin area, or FTP your files directly into the plugin folder.

Next, head to the WordPress admin, and go to the Plugins page. You'll see your plugin with all the details you filled out, ready to be activated. Your functionality will not work until you activate the plugin, and any functionality you have programmed will cease to work if you deactivate the plugin.

WordPress Functionality Plugin

Let's see if it works!

It works!

So it does!

Other options?

I'm glad you asked! There is actually a plugin out there that allows you to create modular plugins without having to upload and activate them - you can do it right through the admin!

Check out the Functionality plugin from Shea Bunge. This plugin makes creating functionality plugins easy and pain-free.

All you have to do is go to Plugins -> Edit Functions, and you have a blank canvas to start adding your new functions! These functions will work regardless of what theme you have selected.

WordPress Functionality

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">