Skip to content

Hooks

config/hooks.php

Contains all the hook classes that should be loaded.

The goal is to group similar actions/filters together for example changing settings/values in a plugin.

All hook classes are located in app/Hook and should implement the App\Lib\Contracts\HookContract interface.

app/Hook/ExampleHook.php
<?php
namespace App\Hook;
use App\Lib\Contracts\HookContract;
class ExampleHook implements HookContract
{
public function register(): void
{
add_action('init', [$this, 'setupNavMenus']);
}
public function setupNavMenus(): void
{
register_nav_menus([
'primary' => 'Primary Navigation',
]);
}
}

All hooks should be registered in config/hooks.php file. This file contains an array of hook class names that should be loaded.

Registering a hook is as simple as adding the class string to the array.

config/hooks.php
<?php
use App\Hook\ExampleHook;
return [
// other hooks
ExampleHook::class,
];