Hooks and Modules
This document explains the use of hooks and modules folders in WP Bones starting from version 1.8. The hooks folder is for files that are automatically loaded when the plugin starts, containing hooks, filters, shortcodes, etc. The modules folder is also introduced for further flexibility. Examples are provided for basic and advanced usage, demonstrating how to create and use PHP files within these folders to define custom hooks and actions.
Overview
To further enhance the flexibility of including external files, starting from version 1.8, WP Bones allows the creation of two optional folders within the plugin folder. These folders are hooks and modules.
Hooks
The hooks folder is intended to hold all the files that we want to be automatically loaded when the plugin starts. These files can contain hooks, filters, shortcodes, etc. Essentially, anything.
Basic usage
To use the hooks folder, you may create one or more PHP files. For example, you can create a file named my-hooks.php and add the following code:
Now the function my_hooks() will be available throughout the plugin.
Advanced usage
You can also use the hooks folder to create a file for each hook. For example, you can create a file named my-action.php and add the following code:
Goodies
If you’re a React developer and love ❤️ hooks, you can also approach PHP programming with the same concept. For example, you can create a file named use-option.php and add the following code:
To use this hook in this way:
or even better
Modules
Modules are similar to hooks, but unlike hooks, they must be explicitly loaded. Modules are also PHP files and must be located within the folder: /plugin/modules/.
To load a module, you can use the wpbones_modules() function. For example, you can create a file named my-module.php and add the following code:
and then load it using:
Advanced use case
The modules can also be used for more interesting and advanced cases like the following.
For example, you can create in the /plugin/modules/my-awesome-function.php
and then load it using:
Goodies
Another example is to create a module that returns an array. For example, you can create in the /plugin/modules/config.php
and then load it using:
you may also use a different syntax:
Of course, you may return any type of data from a module, not just an array. For example, you can return a string, an object, or even a function. For example, you may return an instance of a class:
and then load it using: