WordPress Tutorial: How to Create a WordPress Plugin

Step 5 – Load Plugin Files

WordPress Plugin Load Files
WordPress Plugin Load Files

Steps to Create a WordPress Plugin

  1. Name Your WordPress Plugin
  2. Isolate Your Plugin From Other Plugins
  3. Organize Your Plugin Files
  4. Define Directory Paths
  5. Load Plugin Files
  6. Directory Structure
  7. Plugin Activation, Plugin Deactivation, and Plugin Uninstallation
  8. Essential Plugin Files
  9. Security: Protect Plugin Files
  10. Plugin Hierarchy

In order to implement organizational tactics, you need to conditionally determine and load the files appropriately. This can be done very easily in your main plugin file.

Example of conditionally loading plugin files:

You’ll notice a few things here:

  1. I used an action hook plugins_loaded. The reason for this is that it ensures your plugin functionality is completely available at the same time as all other plugins. This also adds flexibility for you and/or other WordPress developers by allowing them to essentially disable your plugin temporarily or permanently, without deactivating your plugin from WordPress itself.
  2. I used an anonymous function as an attached callback function to the plugins_loaded hook. I did this for a specific reason. I don’t want this function to easily be unhooked from the plugins_loaded action hook. I want to make it possible, but not a simple task. This adds a small level of protection for your plugin, while still allowing flexibility.
  3. I defined two custom action hooks. mbe-plugin-load (at the start of the function) and mbe-plugin-loaded (at the end of the function). This allows for you and other WordPress developers to execute functionality before your plugins has been loaded, and/or after your plugin has been loaded.

Note: None of your plugin functionality starts until the core WordPress action hook plugins_loadedhas been fired. This means that none of your functionality will be available during plugin activation for example, because the plugin hasn’t loaded yet. More information on this in Step 7.

Leave a Reply