WordPress Tutorial: How to Create a WordPress Plugin

Step 3 – Organize Your Plugin Files

WordPress Plugin Organization
WordPress Plugin Organization

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

I’ve developed several WordPress Plugins and WordPress Themes over the years, and I’ve come to the conclusion that organization is KEY. Personally, I like to organize my files by “views” or “scopes”.

Create Sub-directories

With this method of organization, you have: front-end functionality, back-end functionality, and global functionality (both front-end functionality and back-end functionality).

Create the follow sub-directories:

  1. frontend => ./wp-content/plugins/mbe-plugin/frontend
  2. backend => ./wp-content/plugins/mbe-plugin/backend
  3. global => ./wp-content/plugins/mbe-plugin/global

Create Index Files

For each of the above directories, create an index.php file (much like you did for the root of your plugin directory). These files will act as the “master file” which is responsible for that particular “view” or “scope” of your plugin. You’ll want to make sure they’re namespaced appropriately, to avoid conflicting functionality later on.

Contents of ./wp-content/plugins/mbe-plugin/frontend/index.php:

Contents of ./wp-content/plugins/mbe-plugin/backend/index.php:

Contents of ./wp-content/plugins/mbe-plugin/global/index.php:

You’ll notice that in each of the three files, I’ve defined a namespace. Personally, I feel no need to assign a unique namespace for the global “view” or “scope”. In my opinion, the default namespace of the plugin is a sufficient place for the global functionality. The important thing is to separate back-end functionality from front-end functionality.

Leave a Reply