planmacher Posted March 23, 2016 Share Posted March 23, 2016 Hello, I am new to hooks... - so help is very appreciated! Found a lot of examples for hooks but not for hooking from the site/templates/admin.php. What would be the right syntax - for example i have tried this at the top of admin.php: $page->addHookAfter('render', 'testFunction'); function testFunction($event){ // add a "Hello World" paragraph right before the closing body tag $event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return); } But nothing happens. Also tried it with wire()->addHookAfter('Page::render', 'testFunction'); What is wrong??Thanks for helping! Link to comment Share on other sites More sharing options...
netcarver Posted March 23, 2016 Share Posted March 23, 2016 Hello planmacher, Please try this in your <site_root>/site/ready.php file... <?php $page->addHookAfter('render', function($event) { $event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return); }); HTH! 2 Link to comment Share on other sites More sharing options...
planmacher Posted March 23, 2016 Author Share Posted March 23, 2016 Thanks netcarver - this works! Cann you please give me a little explanation about the differences between admin.php and ready.php. Because on "https://processwire.com/api/hooks/" I found this: if you are defining hooks specific to the ProcessWire admin, you should define them at the very top of the /site/templates/admin.php. And that is what I tried to do - adding functionality to admin pages ... Link to comment Share on other sites More sharing options...
netcarver Posted March 23, 2016 Share Posted March 23, 2016 Sorry, I've not tried adding hooks in the admin.php file before so I don't know the perfect answer to this. However, if I wanted to hook just admin page renders then I'd keep my ready.php hook and have it filter on the page's template to see if it was an admin page. Like this... $page->addHookAfter('render', function($event) { $template = $event->object->template; if ($template == 'admin') { $event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return); } }); Which should restrict changes to just the admin pages. 2 Link to comment Share on other sites More sharing options...
netcarver Posted March 23, 2016 Share Posted March 23, 2016 There's an official blog article about ready.php that might help. Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 23, 2016 Share Posted March 23, 2016 admin.php works just fine as well if the hook should really only run on admin pages. 2 Link to comment Share on other sites More sharing options...
netcarver Posted March 23, 2016 Share Posted March 23, 2016 Hi Benjamin, admin.php works just fine as well if the hook should really only run on admin pages. Can you provide an example that shows successful use of the replacement wanted by planmacher as hooked from admin.php? If it is possible, I'd like to learn how to do this too. I must admit, I am a little doubtful about a straight translation from ready.php to admin.php as I think admin rendering works a little differently. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 23, 2016 Share Posted March 23, 2016 Seems like for that specific hook admin.php is called to late in the bootstrap process. But other than the timing there's no disadvantage in using admin.php to hold admin related hooks. 2 Link to comment Share on other sites More sharing options...
planmacher Posted March 23, 2016 Author Share Posted March 23, 2016 Thanks everyone for help and explanation. Everything is working fine with ready.php! So I will use this for my ppurposes... 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now