WordPress Tutorial: How to Create a WordPress Plugin

Step 4 – Define Directory Paths

WordPress Plugin Directory Paths
WordPress Plugin Directory Paths

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

Loading files in a WordPress plugin can be a bit tricky. Get the trickery out of the way early on by determining the directory path location of your plugin and defining a PHP Constant to be used later throughout the development of your plugin. Personally, I like to define a few additional constants to also be used later on. Optional for you, but I’ve found them to be nice to have.

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

Define a PATH constant for each “view” or “scope” of your plugin as well.

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

Output of: \MBE_PLUGIN\FRONTEND\PATH
Would produce: ./wp-content/plugins/mbe-plugin/frontend

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

Output of: \MBE_PLUGIN\BACKEND\PATH
Would produce: ./wp-content/plugins/mbe-plugin/backend

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

You’ll notice in the global ./mbe-plugin/global/index.php file, that the constant is GLOBAL_PATH instead of PATH. That’s because the global files use the same namespace as the main plugin ./mbe-plugin/index.php file. There’s already a constant named PATH defined in the main plugin ./mbe-plugin/index.php file.

If you were to define PATH again under the global ./mbe-plugin/global/index.php file, it would conflict with the PATH constant defined in the main plugin ./mbe-plugin/index.php file. Simple remedy that makes logical sense is to just label it as GLOBAL_PATH.

If you were to output \MBE_PLUGIN\PATH, it would output the path to the root of your plugin directory. If you were to output \MBE_PLUGIN\GLOBAL_PATH, it would output the path to the global files directory of your plugin.

Output of: \MBE_PLUGIN\PATH
Would produce: ./wp-content/plugins/mbe-plugin

Output of: \MBE_PLUGIN\GLOBAL_PATH
Would produce: ./wp-content/plugins/mbe-plugin/global

Leave a Reply