Plugin Basics

Standard
[display-posts post_type="content" id="3672" include_content=true include_title=false wrapper="div" wrapper_class=""]

Plugin Basics

Getting Started #

In the example above, ‘vi’ is the editor. Use the editor that is comfortable for you.

Now that you’re editing your new plugin, you need a name for it. The lines below are all you need for WordPress to recognize your plugin.

1
2
3
4
/*
Plugin Name: Handbook Example
*/

After you save the file, you should be able to see your plugin listed in your WordPress site. Log in to your WordPress site, and click Plugin on the left nav of your WordPress Admin. You should now see your new plugin!

Sometimes a plugin you create is just for your site. Usually though, you’ll want to share your new plugin with the rest of the WordPress community. As part of the standard Plugin Header, you specify a license. This lets the user know how they can use your code. To maintain compatibility with WordPress core, it is recommended that you pick a license that works with GNU General Public License (GPLv2+).

There are more values you can add to your Plugin Header which will help its display in your WordPress Admin. If you’re planning on submitting to WordPress.org though, you’ll need to follow the WordPress Plugin Header Requirements.

Top ↑

Hooks: Actions and Filters #

There are hooks sprinkled throughout WordPress core. WordPress hooks allow you to tap into WordPress at specific points to change how WordPress behaves without editing any core files.

There are two types of hooks within WordPress: actions and filters. Actions allow you to add or change WordPress functionality, while filters allow you to filter, or change, content as it is loaded.

Hooks are not just for plugin developers; hooks are used extensively to provide default functionality by WordPress core itself. Other hooks are unused place holders that are simply available for you to tap into when you need to alter how WordPress works. This is what makes WordPress so flexible.

Two of the hooks you’ll need when creating a plugin are the register_activation_hook() and the register_uninstall_hook() hooks. The activation hook runs when you activate a plugin. You would use this to provide a function to set up your plugin — for example, creating some default settings in the wp_options table. The uninstall hook is used to clean up after your plugin. This runs after the deactivation hook, and deletes the plugin from the user’s WordPress installation.

Additionally, you can add hooks throughout your plugin codebase with do_action, which will enable developers to extend your plugin by passing functions through your hooks.

Adding functions to hooks isn’t the only thing you can do. You can also use remove_action to remove a function that was defined earlier. For example, if your plugin is an add-on to another plugin, you can use remove_action with the same function callback that was added by the previous plugin with add_action. The priority of actions is important in these situations, as remove_action would need to run after the initial add_action.

You should be careful when removing an action from a hook, as well as when altering priorities, because it can be difficult to see how these changes will affect other interactions with the same hook. We highly recommend testing frequently.

You can learn more about creating hooks and interacting with them in the Hooks & Filters section of this handbook.

Top ↑

Use WordPress APIs #

Did you know that WordPress provides a number of Application Programming Interfaces (APIs)? These APIs can greatly simplify the code you need to write in your plugins. You don’t want to reinvent the wheel—especially when so many people have done a lot of the work and testing for you. The most common one is the Options API, which makes it easy to store data in the database for your plugin. If you’re thinking of using cURL in your plugin, the HTTP_API might be of interest to you. Since we’re talking about plugins, you’ll want to study the Plugin API. It has a variety of functions that will assist you in developing plugins.

Top ↑

How WordPress Loads Plugins #

When WordPress loads the list of installed plugins, it searches through the plugins folder and sub-folders to find PHP files with plugin headers.  If your entire plugin is just one file like Hello Dolly, it could be located directly in the plugins folder, but more commonly, plugin files will reside in their own folder, named after the plugin.

 
Skip to toolbar