Hooks and Modules
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:
<?php
/**
* You may use this file to define your own hooks.
* This file, and any other file in this directory, will be automatically included in the plugin.
*/
function my_hooks()
{
echo "Hello, World!";
}
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:
<?php
/**
* You may use this file to define your own hooks.
* This file, and any other file in this directory, will be automatically included in the plugin.
*/
add_action('wp_head', 'my_action');
function my_action()
{
echo "Hello, World!";
}
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:
<?php
function useOption($name, $default = '')
{
$value = get_option($name, $default);
return [$value, function ($v) use ($name) {
update_option($name, $v);
return $v;
}];
}
To use this hook in this way:
list($value, $setValue) = useOption('my_option', 'default value');
or even better
[$value, $setValue] = useOption('my_option', 'default value');
echo $value; // default value
$value = $setValue('new value');
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_module()
function. For example, you can create a file named my-module.php
and add the following code:
<?php
function my_simple_function()
{
echo "Hello, I'm a simple function!";
}
and then load it using:
// NOTE that you don't need to use the .php extension or the path
wpbones_module('my-module');
my_simple_function(); // Hello, I'm a simple function!
WP Bones also provides the
import
function to load modules. Since this might be a common function, it is generally better to usewpbones_module()
. However, if you wish to useimport
, you can do so.
// NOTE that you don't need to use the .php extension or the path
import('my-module');
my_simple_function(); // Hello, I'm a simple function!
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
<?php
return function () {
echo "Hello, I'm a awesome function!";
};
and then load it using:
$awesome_function = wpbones_module('my-awesome-function');
$awesome_function(); // Hello, I'm a awesome function!
Goodies
Another example is to create a module that returns an array. For example, you can create in the /plugin/modules/config.php
<?php
return [
'controller' => 'WPKirk\Http\Controllers\Examples\ExampleController',
'mul' => function ($a, $b) {
return $a * $b;
}
];
and then load it using:
$config = wpbones_module('config');
echo $config['controller']; // WPKirk\Http\Controllers\Examples\ExampleController
echo $config['mul'](2, 3); // 6
you may also use a different syntax:
['controller' => $controller, 'mul' => $mul] = wpbones_module('config');
echo $controller; // WPKirk\Http\Controllers\Examples\ExampleController
echo $mul(2, 3); // 6
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:
<?php
class MyClass
{
public function __construct()
{
echo "Hello, I'm a class!";
}
public function myMethod()
{
echo "Hello, I'm a method!";
}
}
return new MyClass();
and then load it using:
$my_class = wpbones_module('my-class');
// Hello, I'm a class!
$my_class->myMethod(); // Hello, I'm a method!