# One function for multiple hooks

**URL:** <https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 10, 2015, 6:59pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592 "2015-07-10T18:59:20Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SQBiz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sqbiz/32/5014_2.png) [@SQBiz](https://forum.getkirby.com/u/SQBiz)\
**Post date:** [July 10, 2015, 6:59pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/1 "2015-07-10T18:59:20Z")

</div>

Is it possible to write one function for multiple hooks? For instance, something like:

```auto
kirby()->hook(array('panel.page.create', 'panel.page.update'), function($page){
 // ...
});

```

---

<div class="post-metadata">

**Author:** ![PaulMorel](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paulmorel/32/371_2.png) [@PaulMorel](https://forum.getkirby.com/u/PaulMorel)\
**Post date:** [July 10, 2015, 8:30pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/2 "2015-07-10T20:30:40Z")

</div>

I guess you could do this:

```php

function foo($page) {
   // Function stuff
}

kirby()->hook('panel.page.create', foo($page));
kirby()->hook('panel.page.update', foo($page));

```

I haven’t tested this though.

---

<div class="post-metadata">

**Author:** ![SQBiz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sqbiz/32/5014_2.png) [@SQBiz](https://forum.getkirby.com/u/SQBiz)\
**Post date:** [July 12, 2015, 6:15pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/3 "2015-07-12T18:15:52Z")

</div>

That’s what I’m now doing, and it does work

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 17, 2015, 8:45pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/4 "2015-07-17T20:45:43Z")

</div>

I guess it needs to be the following:

```auto
function foo($page) {
   // Function stuff
}

kirby()->hook('panel.page.create', 'foo');
kirby()->hook('panel.page.update', 'foo');

```

You could also use an anonymous function to avoid naming conflicts:

```auto
$foo = function($page) {
   // Function stuff
};

kirby()->hook('panel.page.create', $foo);
kirby()->hook('panel.page.update', $foo);

```

---

<div class="post-metadata">

**Author:** ![AugustMiller](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/augustmiller/32/160_2.png) [@AugustMiller](https://forum.getkirby.com/u/AugustMiller)\
**Post date:** [July 17, 2015, 11:41pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/5 "2015-07-17T23:41:11Z")

</div>

I think @lukasbestle has got it. What was happening in @PaulMorel’s solution was immediate invocation of the function, rather than passing of a `callable` to the hook registry.

This is often a point of frustration in JavaScript, whereby a single set of parens causes the result of a function to be passed (`foo()`), not the function itself (`foo`).

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 18, 2015, 2:46pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/6 "2015-07-18T14:46:53Z")

</div>

Exactly. Immediately invoking the handler doesn’t make sense in this case and will probably spit out a warning about a missing parameter `$page` and later about a non-callable function when the hook handler is run.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 13, 2016, 7:02am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/7 "2016-04-13T07:02:58Z")

</div>

**I don’t get it to work. I tried this:**

```php
$save_revision = function( $page ) {
  RevisionCreate::saveRevision( $page );
}

kirby()->hook('panel.page.create', $save_revision );

```

> Parse error: syntax error, unexpected ‘kirby’ (T\_STRING) in C:\wamp\www\revisions\site\plugins\revisions.php on line 74

**This work:**

```php
kirby()->hook('panel.page.create', function($page) {
  RevisionCreate::saveRevision($page);
});

```

I want to add more than one hook so it would be awesome to get the first one working.

## Update

It’s just a typo! It should be a semicolon after the anonymous function, just like in javascript. Maybe update your answer @lukasbestle. With that is works just fine!

---

<div class="post-metadata">

**Author:** ![FabianSperrle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabiansperrle/32/5426_2.png) [@FabianSperrle](https://forum.getkirby.com/u/FabianSperrle)\
**Post date:** [April 13, 2016, 8:27am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/8 "2016-04-13T08:27:25Z")

</div>

I can’t test it right now, but the original proposed code should work now as well.

> [@SQBiz](#):
>
> kirby()-\>hook(array(‘panel.page.create’, ‘panel.page.update’), function($page){  
> // …  
> });

> <https://github.com/getkirby/kirby/issues/325>
>
> I just can't find a good title for this issue 🙃 
> 
> The biggest problem is that …I can't see the emojis! 🐴 
> 
> Instead I get this:
> 
> !\[image\](https://user-images.githubusercontent.com/19433935/38139378-d14b3972-3437-11e8-8cc6-6cf34b814421.png)
> 
> !\[image\](https://user-images.githubusercontent.com/19433935/38139483-3e380588-3438-11e8-9b5a-dd65c3a14643.png)

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 13, 2016, 9:07am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/9 "2016-04-13T09:07:03Z")

</div>

I did not get it to work.

**Work:**

```php
$save_revision = function( $page ) {
	RevisionSave::run( $page );
};

kirby()->hook('panel.page.create', $save_revision );
kirby()->hook('panel.page.update', $save_revision );

```

**Don’t work:**

```php
kirby()->hook([
	'panel.page.create',
	'panel.page.update'
	], function($page){
		RevisionSave::run( $page );
	}
);

```

**Don’t work:**

```php
kirby()->hook(array('panel.page.create', 'panel.page.update'), function($page){
	RevisionSave::run( $page );
});

```

Just in case, I tried two different ways to write arrays. None of them worked for me.

---

<div class="post-metadata">

**Author:** ![FabianSperrle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabiansperrle/32/5426_2.png) [@FabianSperrle](https://forum.getkirby.com/u/FabianSperrle)\
**Post date:** [April 13, 2016, 9:22am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/10 "2016-04-13T09:22:45Z")

</div>

So, “I will definitely add this to the next release” somehow got overlooked @bastianallgeier? Maybe in 3.0 together with permissions then? 😜

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [April 13, 2016, 9:42am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/11 "2016-04-13T09:42:55Z")

</div>

It should be implemented on the develop branch. Have you tested with the latest Kirby 2.3 beta?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 13, 2016, 10:08am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/12 "2016-04-13T10:08:09Z")

</div>

Considering that the issue was closed in November last year, I would have expected it in the 2.2.3 version; there’s nothing in the changelog of 2.2.3 or 2.3.0 regarding this feature, though (although it might just have been overlooked when writing the changelog)

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [April 13, 2016, 10:17am UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/13 "2016-04-13T10:17:41Z")

</div>

Also the commit is missing. Seems like Bastian implemented it but did not push it. @bastianallgeier

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [April 13, 2016, 1:31pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/14 "2016-04-13T13:31:12Z")

</div>

I can just confirm that I’m using 2.3 beta.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:59pm UTC](https://forum.getkirby.com/t/one-function-for-multiple-hooks/1592/15 "2025-02-06T16:59:47Z")

</div>


