WordPress is an amazing piece of content management software. Through hosting everything from stores to blogs and more, it is being used on numerous websites to run personal sites and even major publications. Yet, behind the simple process of writing posts and pages is often extra functionality in terms of plugins.
Officially defined as “ways to extend and add to the functionality that already exists,” plugins often account for functionality that makes a site stand out from others (WordPress Codex).
Writing Your First Plugin
To start writing a plugin, all you need is a name. This can be as complex as “My Super Awesome Explosion Plugin Extreme” or as simple as “My First Plugin.” Regardless, this name is the bare minimum required to make a plugin a plugin.[gist https://gist.github.com/videlais/071efef6fd91c92d1a75 /]
Note: Looking at the code above, you may have noticed that plugins, like WordPress itself, are written in PHP. This means that, to write a plugin, you will have to write some code in PHP, too. However, where you write it is entirely up to you. There are some great tools out there for helping you write PHP.
Often, when a plugin is activated, something will happen. This means, at some point, the developer used the register_activation_hook to bind some function to that event.[gist https://gist.github.com/videlais/6baf0e58f98d7b609bae /]
Note: Line 6 in the above code is especially important. As the “Writing a Plugin” page warns, you should protect against your plugin being run directly in most cases. To prevent that, simply include a check to “kill” the script when it is tried.
Adding Functionality (i.e. doing a thing)
While the WordPress Codex for plugin development has substantial documentation on doing many things, one of the simplest actions to do in PHP is to simply echo a string.[gist https://gist.github.com/videlais/6d5373d3550d3e4b1c97 /]
Note: Line 11 in the above code contains the function add_action. Like register_activation_hook, add_action registers an event with WordPress. When it happens, as in the above code with ‘wp_footer’, the action call the function registered.
Testing the Plugin
Finally, after writing a plugin, some testing is required.
After logging in to the Dashboard, go to Plugins and click on the “Add New” link.
After zipping the plugins file(s), choose it from the file dialog and click on the “Install Now” button.
After it is installed, activate the plugin. Visit any post or page to see the new change.