A Plugin-in is a piece of software or custom code that extends an application. Reich Web Consulting specializes in creating custom WordPress plugins, for example.

Posts

Creating a WordPress Plugin, Part 1: Naming Your Plug-in

[amazon_enhanced asin=”0470916222″ /]

The first and arguably most important step in creating a WordPress Plugin is choosing a name for it. If you plan to share your plug-in with others it should obviously be catchy, but more importantly it should also be unique for two very good reasons explained below.

Make Your Plugin Stand Out

WordPress has a vast number of free and commercial plugins available. If you choose to share your plug-in with others you’ll want your plug-in to stand out from the rest. Before you decide on a name for your plug-in, use Google and the WordPress Plugin Directory to do some research. Rather than call our recipe plug-in that we’ll be creating simply Recipes, we’re going to call it Delicious Recipes, by Reich Web Consulting.

Namespacing

Regardless of whether or not you choose to share your plugin with others, when you begin coding you’ll have to make decisions about naming things like functions, classes, constants, and other identifiers.  Identifiers must be unique not just within your plugin but to PHP, WordPress, your active theme, and any other active plug-ins.  Defining two identifiers with the same name will result in a namespace collision: an error condition in which two or more identifiers have the same name.  Lets say we’ve written the following PHP code:

// Intentially cause a namespace collision: 
function printf( $args ) { 
    // Function definition
}

Because the printf() function already exists in PHP, the following error is generated:

An example of a PHP namespace collision error.

Using the PHP and WordPress documentation you can safely avoid namespace collisions with PHP and WordPress, but how do you avoid colliding with the thousands of other plugins and themes that you and others might install alongside of your own? The answer is to namespace your plugin code. 

How do you namespace your plug-ins?  It’s as simple as choosing a prefix that’s presumably unique to your plug-in and appending it to all global identifiers. The name of my company is Reich Web Consulting and the plug-in we will be developing is for managing recipes, so I’m going to choose RC_Recipes as my namespace:

<!--?php /*  * Define a namespaced class.  */ class RC_Recipe_Plugin {      // Class code } /*   * Define a namespaced function.    */ function rc_recipe_plugin_function( $args ) {      // function code  } /*   * Define a namespaced constant.   */ define( 'RC_RECIPE_CONST', 0 ); ?-->

If you’re experienced with PHP you might be wondering why we don’t solve the problem of namespacing by using PHP’s built-in namespace feature. The reason it’s not used is because, at of the time this tutorial was written, WordPress requires version 5.2.4 of PHP, which does not support namespaces. While you could still write your plug-in using PHP namespaces you would be seriously limiting the number of people that could use it by requiring a version of PHP newer than that which WordPress itself requires.

Summary

Before you go all gung-ho slinging code it pays to takes a few minutes to consider the design and marketing implications of naming your plugin.  Choose something unique but catchy, and use your plug-in name to create a unique namespace for your code.   In the next part of this tutorial we’ll be creating the directory and file structure for out plugin.  Catch you then!

Choosing a WordPress Backup Plugin

One of the first things you should do after installing a new WordPress blog is configure backups. Though WordPress does feature an export feature that allows you to save much of your content to XML, there isn’t a full-blown backup feature built into the WordPress Core. You’ll need to download a WordPress backup plugin.

Features to Look For in a WordPress Backup PlugIn

These are the minimum features that I look for in a backup plug-in.  You may have different requirements than my own but if your backup plug-in doesn’t provide these features, you will eventually find it lacking.

  • Backs up the entire database, not just the core WordPress tables. In the event that your site crashes and needs to be restored, you’re going to need a backup of your database.  The last thing you want is to restore your database and find that plug-in content such as purchase records in a shopping cart plugin or images stored in a gallery plug-in no longer exist.
  • Backup the entire WordPress Directory. Some backup plug-ins only backup the database, but there is plenty of content that’s stored on the filesystem and not the database including uploaded media, plugins, and theme customizations. Choose a plugin that backs up the entire file system for your blog. That way when a crisis occurs, you can simply dump the files and database back to the server and continue business as usual.
  • Scheduling. Your backup plug-in should provide some sort of scheduling feature that can automatically perform backups as a daily, weekly, or monthly task. You shouldn’t have to remember to manually backup you blog, though a manual backup feature is helpful as well.
  • “Off-Site” Backup.Your backup plug-in should have some way of saving your backup off site. It could be by emailing them to you, uploading them to an FTP site: anything that duplicates your blog data to a second server in case your server is physically damaged. A plug-in that simply saves the backup to the same server that hosts your blog doesn’t protect you from the all-to-common hard drive failure, your hosting company going belly-up, or any other scenario that prevents you from getting access to  your data.

Backing Up WordPress to DropBox

I use DropBox to store business records and other important files, and so I’ve settled into using the WordPress Backup to DropBox plugin for most of my sites.  The plug-in provides all of the features that I mentioned above.  In addition it gives me access to those backups from my PC via the DropBox desktop application, and gives me confidences that my backups are occurring when I scheduled them because I can see the files updating in real-time.

Another excellent and popular plugin is Backup Scheduler. It allows you to specify what gets backed up (database, files, or everything), and has the option to send you the backup via email.