Thursday, March 28, 2024

WordPress For the Web Developer: How To Write Your Own Plugins

Previously in this tutorial series about the WordPress Content Management System, we’ve covered relatively simple ways of modifying the look, feel, and functionality of your WordPress website by using plugins and selecting and modifying themes. Now you’ll be getting deeper into the internals of WordPress by learning how to write your own plugins and integrate them into WordPress.


Here’s the high-level overview of the process:


  • Select a unique name for the plugin and main function
  • Create directory, main and readme files with header and license information
  • Write and debug the code (easy, huh? 🙂
  • Zip up the code directory, and install plugin on WordPress for final testing

Name That Plugin


All WordPress plugins must have a name, but more importantly, the name must be unique across all WordPress plugins, to prevent the wrong code from being executed. So before you get too involved in writing code, take a few minutes to search the currently available plugins and pick a good, descriptive name that nobody else is using.


To stay compatible with the main WordPress system, plugins are coded in PHP (if you’re not up to speed on PHP, please check out our excellent tutorial series here). Like the name of the plugin itself, the main PHP file containing the plugin code should also be named descriptively, as well as not matching the name of any other WordPress plugin file.


If you’re writing a very simple plugin, or one you don’t plan to release to anyone else, this single PHP file may be all you’ll need in the end, and like any other plugins, it will be installed into the wp-content/plugins/ folder. However, in most cases you’ll be better off setting up a directory structure. That way you can have something like this under wp-content/plugins/:


myplugin/
myplugin/myplugin.php (mandatory; the main plugin code)
myplugin/readme.txt
myplugin/screenshot.png
myplugin/images/
myplugin/css/
myplugin/html/
myplugin/js/

All the extra files and subdirectories are optional, but if you need them, it’s much more clear and professional to keep them organized in a way similar to this. It doesn’t create any additional work or problem for the eventual user, because plugins are commonly distributed as ZIP files which preserve the directory structure.
In order to function with the WordPress plugin system, your plugin must also have its name declared in a header at the very beginning of the file. Assume this is myplugin.php:

<?php
/*
Plugin Name: My Plugin
*/
?>

This may not do much (and in fact, does nothing), but it is a legal plugin and if properly installed in the plugin directory, it will appear on the Manage Plugins page. Normally, though, you’d want to add some more header information, using this format:

Plugin URI: http://PluginHomePage.com
Description: Plugin description in a few words or more
Version: Version number such as 2.3
Author: Your Name (if you wrote it)
Author URI: http://YourHomePage.com
License: GPL (or whatever license terms you choose)

Just to demonstrate the basic structure, let’s write a complete simple plugin, called catify.php. By now, you should be able to copy/paste this into a file, install, activate, and try it on your own.

<?php

/*
Plugin Name: Catify
Description: This plugin finds the word “dog” in text and replaces it with “cat”
*/

function catify($text) {

     $text = str_replace(‘dog’, ‘cat’, $text);

     return $text;
}

add_filter(‘the_content’, ‘catify’);
?>


Catify is defined as a PHP function, taking $text as an argument, and then simply replacing every instance of the string “dog” with “cat” and returning the now-modified string. The next line is pure WordPress: similar to JavaScript, plugins can work with either actions or filters. Actions take place at predetermined times, and your plugin can therefore execute at one of those times. Filters allow a plugin to operate on text, before the text is either displayed or added to the database.


By using the syntax


add_filter(‘select_text’, ‘function’)

we’re telling the plugin to make the named function operate on the text we’ve selected. In this case, the_content refers to the entire post content.


At this point you’ll have to refer to the WordPress API documentation to go much further. This is a link to the general plugin documentation, and here is a list of plugin resources. The WordPress Plugin API page is relatively easy to find, but the lists of action and filter references are not as obvious. All this is a good start; the next article will help you learn how to make more functional plugins.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured