function hook_init

You are here

7 system.api.php hook_init()
4.6 core.php hook_init()
4.7 core.php hook_init()
5 core.php hook_init()
6 core.php hook_init()

Perform setup tasks for non-cached page requests.

This hook is run at the beginning of the page request. It is typically used to set up global parameters that are needed later in the request. When this hook is called, the theme and all modules are already loaded in memory.

This hook is not run on cached pages.

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

See also

hook_boot()

Related topics

14 functions implement hook_init()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

actions_loop_test_init in modules/simpletest/tests/actions_loop_test.module
Implements hook_init().
dblog_init in modules/dblog/dblog.module
Implements hook_init().
hook_language_init in modules/system/language.api.php
Allows modules to act after language initialization has been performed.
locale_init in modules/locale/locale.module
Implements hook_init().
locale_test_init in modules/locale/tests/locale_test.module
Implements hook_init().

... See full list

1 invocation of hook_init()

File

modules/system/system.api.php, line 1882
Hooks provided by Drupal core and the System module.

Code

function hook_init() {
  // Since this file should only be loaded on the front page, it cannot be
  // declared in the info file.
  if (drupal_is_front_page()) {
    drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');
  }
}

Comments

To follow the above suggestion :

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

CSS files can be added to a .info file using the following format:

stylesheets[all][] = node.css

(taken from node.info in node module)

See the developer's guide: "Writing .info files (Drupal 7.x)" for specifications on the .info file.
Also see the D6 theming guide: "Adding style sheets".

Even core themes may be subthemed: Creating a Subtheme. I used this technique to add a CSS file for admin functions on a site with the Seven admin theme.

In D6 you cannot add stylesheets in the .info file. So contrary to the documentation recommendation, you may have to use hook_init.

This documentation is for D7 only. Here you can found D6 documentation about hook_init.

Although you are right with that (there is a different page for D6), this is not really obvious unless checking the URL:

Versions:
4.6 – 8 hook_init()

This should be fixed, btw, and either split properly or merged to just one page, eventually with aliases.

Yes, that is basically right. And somehow, again, not.

hook_init() will be invoked on every full bootstrap. Even if your module does not have anything of worth to add to the actual page request. This results in unnecessary processing time unless your module is involved in literally *every* page request. I could tell of few modules that really do so, block maybe one of these rare.

So in most cases, it is a good idea to add javascript exactly when it is needed. Assume your module implements a block that will only appear on user profile pages (even more, only for users with the role "foo") and contents some fancy jQuery effect based random tips. That said, why would you want to have not just all jQuery stuff added, but also an additional function call and, in the worst case, even unnecessary database queries or other stuff on literally every request, be it even just an AHAH callback for other modules?

Just imagine an average community site with, say, an average of 50 parallel visitors and 20 contrib modules enabled each of which just adds a few JS variables and calls for one or two string translations. Things sum up really quick.

And this is why there are many warnings to not use hook_init unless really inevitable.

As for the example above, the best place for dispatching all effect related javascript files and setting related to this block, will likely be in hook_block and only when $op is set to 'view'.

This is just an example, but hopefully it helps figuring out a bit.

External Javascript can not seem to be added through the info file using the line 'scripts[] = http://yada.com/stuff'

You can just add a script tag to your template file ( page.tpl.php in D6 and html.tpl.php in D7).
LIke this for example:

  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR-MAPPING-KEY&sensor=false"></script>

This hook is not run on cached pages.

I'm the maintainer on a drupal module called Splashify.

To allow the splash page to show up in the correct scenarios where anonymous page caching is on, we had to move a lot of the logic from PHP to JS. However, there is server side check that happens via hook_init() that looks at the page that is being loaded and determines if this is a page where we might want to display the splash page.

To test this out, I added a debug() call after this php code in hook_init(). I verified that this gets hit when viewing the page as anonymous user and when logged in.

So I don't know if this text is just out of date or if there is something else going on....but this is my experience.

This fact shot me in the knee today...