Require hooks in a seperate file

To tidy up my plugin i am trying to require several things like translations, functions and whatsoever in seperate files.

when i am trying this with hooks it seems like the hook is not firing anymore…

// index.php
    'hooks' => [
        require __DIR__ .'/registry/hooks/hook.delivery.php',
    ],

// hook file
<?php return [
        'page.update:after' => function ($newPage, $oldPage) {
          //... hook stuff
        }
] ?>

i am doing something wrong or does that just not work with hooks?

It has to look like this in my opinion.

// index.php
'hooks' => require __DIR__ .'/registry/hooks/hook.delivery.php',

i will have to add all within one file or could i have seperate files for each type of hook?

If you want to split them into different files you need to use array_merge.

// index.php
    'hooks' => array_merge(
        require __DIR__ .'/registry/hooks/a.php',
        require __DIR__ .'/registry/hooks/b.php',
        require __DIR__ .'/registry/hooks/c.php'
    ),

Or if there’s only one hook per file:

// index.php

'hooks' => [
    'page.update:before' => require __DIR__ . '/registry/hooks/a.php',
    'page.update:after'  => require __DIR__ . '/registry/hooks/b.php',
]
// hook file

return function ($newPage, $oldPage) {
    //... hook stuff
};
1 Like