Skip to content

Commit f967a9e

Browse files
📘 Add usage in README.md
1 parent 8ec8599 commit f967a9e

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WordPress hook
1+
# WordPress hook autoloader
22

33
![GitHub Release](https://img.shields.io/github/v/release/dimitriBouteille/wp-module-hook) [![Packagist Downloads](https://img.shields.io/packagist/dt/dbout/wp-module-hook?color=yellow)](https://packagist.org/packages/dbout/wp-module-hook)
44

@@ -11,7 +11,7 @@ WordPress module developed for developers who want to quickly add WordPress hook
1111

1212
This documentation only covers the specific points of this library, if you want to know more about WordPress `action` or `filter`, the easiest is to look at [the documentation of WordPress](https://developer.wordpress.org/plugins/hooks/).
1313

14-
You can find all the documentation in [the wiki](https://github.com/dimitriBouteille/wp-module-hook/wiki).
14+
You can find all the documentation in [the wiki](https://github.com/dimitriBouteille/wp-module-hook/wiki) or [here](#usage).
1515

1616
## Installation
1717

@@ -36,6 +36,40 @@ In your PHP script, make sure you include the autoloader:
3636
require __DIR__ . '/vendor/autoload.php';
3737
~~~
3838

39+
## Usage
40+
41+
Before creating your first hook, you must initialize the module. It is advisable to add this code at the beginning of the `functions.php` file of your theme or in a `mu-plugin`.
42+
43+
```php
44+
$loader = new \Dbout\Framework\Hook\HookLoader(
45+
directory: get_template_directory() . '/hooks',
46+
);
47+
48+
$loader->register();
49+
```
50+
51+
Now you have initialized the module, you just need to create your first action in the hooks folder in your theme.
52+
53+
```php
54+
use Dbout\Framework\Hook\Attributes\Action;
55+
use Dbout\Framework\Hook\Attributes\Filter;
56+
57+
class MyFirstHook {
58+
59+
#[Action(name: 'wp_dashboard_setup', priority: 999)]
60+
public function setupDashboard(): void
61+
{
62+
// Do something
63+
}
64+
65+
#[Filter(name: 'crontrol/filtered-events', acceptedArgs: 2)]
66+
public function filterEvents(array $filtered, array $events): array
67+
{
68+
// Do something
69+
return $filtered;
70+
}
71+
}
72+
```
3973

4074
## Contributing
4175

0 commit comments

Comments
 (0)