Jump to content

Hook in site/templates/admin.php


planmacher
 Share

Recommended Posts

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

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!

  • Like 2
Link to comment
Share on other sites

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

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.

  • Like 2
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...