WordPress Tutorial: How to Create a WordPress Plugin

Step 8 – Essential Plugin Files

WordPress Plugin Essential Files
WordPress Plugin Essential 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

The more you develop with WordPress, the more you will come to realize that hooks are absolutely EVERYWHERE in the WordPress ecosystem. There are two types of hooks in WordPress: action hooks and filter hooks. Whether you use an action hook or a filter hook; they both require an attached callback function, in order to trigger additional functionality on any particular event.

Before you know it, you’ll have SEVERAL hooks used through out your plugin. For every hook you’ll have at least one function, plus any other extended functionality you have.

If you’re not careful, your code can spiral out of control and things can get quite messy very rapidly. That’s why it’s so important to stay on top of your organization.

Create a file named hooks.php and file named functions.php under each includes directory.

FRONTEND

  1. hooks.php => ./wp-content/plugins/mbe-plugin/frontend/inc/hooks.php
  2. functions.php => ./wp-content/plugins/mbe-plugin/frontend/inc/functions.php

Contents of front-end hooks.php:

Contents of front-end functions.php:

BACKEND

  1. hooks.php => ./wp-content/plugins/mbe-plugin/backend/inc/hooks.php
  2. functions.php => ./wp-content/plugins/mbe-plugin/backend/inc/functions.php

Contents of back-end hooks.php:

Contents of back-end functions.php:

GLOBAL

  1. hooks.php => ./wp-content/plugins/mbe-plugin/global/inc/hooks.php
  2. functions.php => ./wp-content/plugins/mbe-plugin/global/inc/functions.php

Contents of global hooks.php:

Contents of global functions.php:

Load hooks.php and functions.php for each “view” or “scope”

At this point, you’ve prepared your files, but they don’t do anything, because they haven’t been included yet. You need to load the files from each “master file” created in Step 3.

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:

Now every time you want to hook into something, you can place the hook in the appropriate hooks.php file and define the callback function in the appropriate functions.php file.

Leave a Reply