Create a simple plugin that demonstrates the minimum requirements for any WordPress plugin. This is the foundation on which you will continue with your plugin development adventures.
So with that in mind, let's create a simple plugin using a single PHP file. So we create a new file, and first we need a name for this plugin, let's name it simple-example-plugin, so we go to File, Save As, simple-example-plugin, and save it in the plugins directory. Because this plugin always will be just one file, it's fine to save it directly in the plugins folder as is.
But let's say that we may want to add more files later on, to further develop the plugin with new features. So just to be safe, let's go ahead and put the plugin file inside of its own directory. And now we can move our plugin file into its own directory like so, this enables us to add more files later, and is just good practice for any size plugin.
Next, every plugin must include a proper file header. To save time, we can grab an example file header from the exercise files for this video. We want to add the file header to the beginning of the main plugin file, as we have done here. The file header contains metadata about the plugin, like the plugin name, description, author and so forth. At the very least, the plugin file header must include the name of the plugin. Every WordPress plugin must include a file header located at the beginning of the main plugin file.
We'll look at a more complete file header a bit later in the course, for now, we'll keep things simple with just this basic example. If ever in doubt, just look at the file header for the Akismet or hello-dolly plugins to get a better idea. For the plugin license, we want to keep things open source, just like WordPress itself. Including a proper license lets users know that it's OK to use your code. Most WordPress plugins are licensed under the GPL, GNU Public License, version two or better, this is the same license that's used for the WordPress core files.
You can learn all about the GPL at the official website. Basically, the GPL ensures that WordPress and all of its plugins and themes will remain open source for everyone, so it won't get locked down by proprietary restrictions and legal complications. Now to make the license complete, we need to add a copying permission statement to the file header, we can grab a complete example from the exercise files. We want to add the statement to the file header like so. Additionally, we should include a copy of the GNU license itself, which also is provided in the exercise files, so we navigate to the WordPress plugins directory and add the license file to our plugin.
For more information about how to apply the GPL, visit gnu.org. So at this point, our example plugin is installed and ready to activate. Of course, this simple example doesn't actually do anything, but it does give us a good starting point to see how to begin developing our own plugins. Here in the WordPress admin area, we visit the Plugins page, here we can see our example plugin ready to go. Notice how it uses the file header information for the plugin name, description, version, author, and so forth.
This is one reason why it's important to include a proper file header, so your plugin looks complete when displayed here on the Plugins page. Now let's go ahead and activate our new plugin, and presto, our first plugin is now activated and ready to go. From here, we can add code to make our plugin do whatever we want, as we'll see in upcoming videos. Now when it comes to installing plugins, there are two possible locations. We can install plugins in the plugins directory, which is the typical location, or we can install plugins in the mu-plugins directory, this directory is for must-use plugins, any plugins installed in this location will be activated automatically.
Must-use plugins have some pros and cons to consider. For the pros, must-use plugins are always activated, so there is no need to log in to the admin area. Also, must-use plugins are loaded before normal plugins. And for the cons, there are no automatic or one-click updates, and there are no update notifications. Also, activation hooks do not work with must-use plugins. And, must-use plugins must be single PHP files, if the plugin file is located in its own directory, it will not work.
To see how must-use plugins work, let's go ahead and move our plugin to the must-use directory. After deactivating the plugin, we return to the code editor, and move only the main plugin file to the must-use directory, like so. Our plugin is now a must-use plugin, and already is active on the site. Here we can see our plugin under the Must-Use tab, where it is active and executed automatically with each page load. We won't be using the must-use directory for this course, but it is something that you should be aware of.
For this course, we're going to focus on typical WordPress plugins installed in the plugins directory, this is where all of the action is happening for most WordPress plugins. Before wrapping up this video, here are some things to keep in mind when choosing a good name for your plugin, these guidelines ensure maximum compatibility with WordPress and other plugins. First, the plugin name should match the main plugin file, and the plugin name should match the main plugin folder. Also, always do your research before choosing a name. Basically, do your research, keep it simple, and be consistent.
In this tutorial, we created a simple plugin that demonstrates the minimum requirements for any WordPress plugin. This is the foundation on which we'll continue with our plugin development adventures.
- WordPress APIs
- Action and filter hooks
- Activating and deactivating plugins
- Plugin security
- Creating the directory and files
- Adding menus and the settings page
- Inserting custom functionality
- Testing and debugging WordPress plugins
- Creating widgets
- Managing users and roles
- Adding custom post types and taxonomies
- Working with custom fields and database queries
- Using APIs: Transients, HTTP, and REST