We will be working on a series of articles focus on “how to do X with WordPress“. These articles will be a little lengthy and they are mainly for experienced webmasters and developers; in case you are new to WordPress, please check out my Guide To Your First WordPress Blog.
And now, some basic introduction.
Expanding on WordPress Features By Developing a New Plugin
WordPress is the most popular content management platform in the world, with more than 60 million users of its self-hosted PHP installation package and millions more using its web-based, remotely-hosted WordPress.com service. This is not a coincidence or mistake. WordPress is, quite simply, the most fully-featured, affordable (it's free!), easily-usable content management application in the world. However, even this titan of content management isn't absolutely perfect, and many users eventually find that the software lacks one or two essential functions that they simply cannot live without.
Enter WordPress plugins.
These unsatisfied users will then go on a quest, a large search, in an effort to find the right plugin to get the job done. Some people find that needed plugin, BUT many others simply don't find that they're looking for.
Some WordPress users might be content to simply give up the fight and resign themselves to a life where one essential feature is missing from their website. But a sizable group of these users is simply not willing to throw in the towel, and they'll pursue the process of designing their own custom-made plugin to get the job done. It might sound complicated and intimidating, and some parts of it might be. Certainly, solid knowledge of PHP will be required for more advanced plugins that add real features to the WordPress functionality. But a plugin is a relatively easy thing to write, especially for basic functions that are standard throughout WordPress and simply need to be “reworked” in order to do the job better and present it in a more pleasing way.
Users who won't settle for second-best, and are thinking about creating their own plugin, need only follow the simple steps below to put their WordPress installation on a path toward greater functionality and compatibility with their big ideas and grand plans for Internet domination.
Step 1: Create a New File within the Plugin Directory and Give it a Purpose
The first step to creating a plugin is to simply create a new PHP plugin file within the existing WordPress plugins directory.
By now, most users should know where to locate that directory as they've likely uploaded a handful of plugins to enhance their WordPress installations' functionality already. However, for those who are really approaching this process as a complete and total novice, the directory containing all pug in files is found at the following location for every version and iteration of the WordPress software:
Navigate to that directory and create a new (blank, at this point) file named “my-custom-plugin.php.” This is the file which will contain all o our work from start to finish, and we'll give it an obvious name sot hat it doesn't accidentally get deleted or prematurely activated during the development process. Once this file has been created and uploaded to the server, use an FTP client to directly edit this file (using a built-in text editor) while it is still hosted on the server. This is the best way to edit remote files, as it makes saving and uploading essentially automatic, and definitely easier than managing multiple versions of the same file stored in different locations.
The plugin will now be named, described, and given a “byline.” This is the same type of information that is placed at the top of a theme's “style.css” stylesheet file, and theme developers will feel right at home by filling out the information below. Place this information into the top of the plugin file before any other code. Even the opening PHP tag should not be placed before these commented lines. If these lines are placed incorrectly, or are omitted entirely, the plugin itself will not appear within the WordPress Dashboard. That means it cannot be activated, used, tested, or debugged, and that's no good. Here's what needs to be filled out before opening the file and proceeding to define a function:
Plugin Name: A Custom Plugin Used to Learn the Ways of WordPress
Plugin URI: http://www.your-website-here.com
Description: This plugin is being used to demonstrate how PHP code, WordPress variables, XHTML, CSS, and other information, is placed into a standard plugin PHP file to add new functionality to a standard WordPress installation.
Author: Your Name Here
Author URI: http://www.your-website-here.com
With that information having been completely filled in, the plugin will now be spotted by the WordPress Dashboard and its information will be properly displayed alongside other plugins within the plugin activation / deletion administration panel. The plugin has now been given a filename, a friendly name, and a purpose. All it's missing at this point are some functions. That's where step two comes in.
Step 2: Defining a Plugin Function (or Functions) for Use on a WordPress Website
This plugin demonstration will assume that the developer has created a custom field. This field will be used to determine whether or not social media links are included with the post so that users might share the post with their friends and family members who are subscribed to their social networking updates on sites like Twitter, Facebook, LinkedIn, and MySpace. A custom field has been created, in this case, named “social_links.”
Using the PHP code in the plugin, we will determine whether or not the social networking field has data inside of it. If it does, a “bar” of social media sharing links will be displayed. If it does not have any data within it, then these links will be omitted. This is a great way to disable the sharing of posts on your website which are simply not meant for viewing by the wider public, and doing it in a plugin is a great way to ensure that this functionality applies across all themes, rather than simply in one theme's entry template.
This process is accomplished by “adding” an action to the WordPress code. This is done using the “add_action” PHP command, parked with several “echo” statements and existing lens of WordPress code that can be ported in directly from existing WordPress template files. Remember that virtually any variable which can be used in a template is also valid for inclusion within a plugin file, although the opening and closing PHP tags must be omitted as the code is already wrapped within one, all-enompassing opening and closing PHP tag enclosure.
Here's what the added “action” looks like within the plugin file when it is completely filled out:
$ID = $wp_query->post->ID;
if (get_post_meta(13); == 1)
return $post_ID . “Social Networking Link Buttons”;
return $post_ID . “”;
The first lines of code are simply used to determine whether or not any “meta” information has been filled out in the WordPress Dashboard for the specific entry being viewed. This is done by querying the WordPress database (using the $wp_query variable) and looking for information within meta field number 13. Each custom field is given a numerical identification, just like entries, comments, and pages. This number can be used within variables for conditional statements.
So, first the plugin looks into the custom field for each entry and sees whether or not content is present in the field's database table. Next, it runs through the conditional. If the content equals “1,” it proceeds. The usage of “1” in this case means “if the field has data present.” The inverse of this would be “0” which would translate to “if the field has no data present.” So, if the field has data present, or equals “1,” the function proceeds to the first part of the conditional. That section provides for the display of social media sharing links. For the purpose of brevity, those links have not been included with the function. However, they can be pretend using simple XHTML within the quotes that follow the “return $post_ID” statement.
If the custom field does not have data present, it effectively equals “0” as far as this plugin's code is concerned. However, because this conditional is an either / or statement, instead of one with many different outcomes, we simply use the “else” construction to instruct the plugin to print the alternative content under any other circumstances. In this case, it simply prints nothing. For this reason, all that needs to be placed within the function is:
return $post_ID “”
At the bottom of the statement, we add our new function as an “action” to the existing “the_content” PHP variable. This will append the social network sharing buttons to the bottom of an entry directly after the entry's content is finished printing. The statements in this parenthetical statement are chronological, meaning the new action (“social_links”) could be placed before “the_content” if those buttons were supposed to be displayed at the beginning of the content, directly after the entry's title has been printed onto the screen.
With the function completed, and the action added to the WordPress Loop directly after the entry's main body of content is printed on tot he page, the plugin is essentially complete. This is the appropriate time to add the closing PHP tag, save the file, and upload it to the server for use.
Step 3: Activate the Plugin and Try it Out
The most important part of the process is activating the plugin and verifying that its functions work as expected and do not cause issues with any existing plugins or standard WordPress features. This is also a good time to verify the integrity of the PHP code and ensure that any output (XHTML or otherwise) prints as desired.
If any errors do arise during the activation and testing process, it's important to check the plugin file and make sure that all statements are properly opened and closed. This means checking for punctuation, in many cases, including parentheses, semicolons, brackets, and lowercase letters. Remember that PHP is case sensitive, and it's also punctuation-sensitive: Any unclosed statement essentially continues infinitely and can cause pages to simply not load, or load extremely slowly.
When all of the kinks have been worked out, the job is largely complete. However, there are a few additional considerations to keep in mind for other scenarios.
Step 4: Creating a Plugin ReadMe File
If the plugin is intended for private use only, there is simply no reason to create a “ReadMe” file unless, of course, you're afraid that you might forget what the plugin does, how it was built, or how it functions within the WordPress Loop itself. However, many developers create plugins with the intent to distribute them to the wider WordPress community and, with more than 60 million users, this is a lucrative way of promoting a website and earning secondary income.
Those users are virtually required to craft a ReadMe document that outlines the plugin's functions, how to install the file to the server, and how to set it up and customize its functions. This file can also contain information such as change log which details every change and update made to the plugin over time. However, the important thing is to explain what the plugin does, how it works, and how to install it. Novice users won't know what to do if they're not guided through the process, much like novice plugin developers didn't know how to create a new WordPress function until the last step of this guide.
When the plugin file has been completed and paired with a ReadMe file (typically a .txt file), it's time to put both documents into their own folder and compress them using the standard ZIP file format. The plugin can then be uploaded to the WordPress Extend community of plugins and themes, as well as to the developer's server for a self-hosted solution.
Relatively Easy to Do, Especially with WordPress Variables and Functions
The great thing about developing a new plugin for WordPress is that the file can use any existing PHP function, variable, or loop, which WordPress has already defined and initialized. This makes it easy to extend WordPress features and enhance their functionality across many themes, and it makes the plugin development process as easy as creating a new theme for the software itself.
Always remember to test for bugs, validation errors, and function conflicts before putting the plugin into active use or sharing with the wider WordPress community. When that process has been completed, and the plugin is working properly, the development stage is complete and you'll be free to enjoy the success and new features you just created. ;)
Further readings on WHSR
To dig deeper on this, check out Jerry's List Of 20 Essential WordPress Plugins, 25 Handy Code Snippets For WordPress Developers, Most Wanted WordPress Hacks, Tips, And Tricks; and, if you are looking for a good WordPress hosting, check out what I've been using recently – WP Engine – it's quite an expensive host (thus not recommended to newbies) but you get what you pay for.