4.6.x core.php hook_init()
4.7.x core.php hook_init()
5.x core.php hook_init()
6.x core.php hook_init()
7.x system.api.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 files that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

See also


Related topics

15 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().
common_test_init in modules/simpletest/tests/common_test.module
Implements hook_init().
dblog_init in modules/dblog/dblog.module
Implements hook_init().
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()


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


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');


manimejia’s picture

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".

NancyDru’s picture

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.

lilwiki’s picture

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

danillonunes’s picture

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

doitDave’s picture

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

4.6 – 8 hook_init()

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

doitDave’s picture

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.

flickerfly’s picture

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

rooby’s picture

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>
chrisroane’s picture

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.

axel.rutz’s picture

This fact shot me in the knee today...

bfuzze9898’s picture

Just want to give people a heads up about the dangers of NOT using this function.

I had some custom-module initialization logic calling Drupal core functions inline instead of using this function. This caused some serious problems. I was calling taxonomy_term_load which in turn calls get_entity_info. When flushing the cache, this call was rebuilding the entity-info definition before all modules were finished loading. Ugh.


lolandese’s picture

Twitter / davereid: Drupal 7 protip: Adding CSS or JS to every page? Use hook_page_build() and not hook_init().

2pha’s picture

I wanted to add js settings to all pages and knew I shouldn't use hook_init. Thank you for pointing me in the right direction.

sanduhrs’s picture

mpotter’s picture

Would also be good to note in the docs that hook_init() is not run during update hooks! So if you are writing a hook_update_N(), do not depend on anything that might require hook_init. Specifically found a module that has a hook_node_update() that assumes it's module hook_init() has run, so in our update hook when we do node_save we have a problem.

sidheswar’s picture

This hook function was removed in drupal 8.
Do check https://www.drupal.org/node/2013014

Mingsong’s picture

Just found a really strange issue that causes an error said 'Fatal error: Call to undefined function user_access()'.
The Flush Page Cache module (https://www.drupal.org/project/flush_page_cache) needs to call user_access function in their hook_init function. Somehow, the 'Fatal error: Call to undefined function user_access()' show up when we flush a page.

I don't know why this happens. But to fix that error, I add following codes in my module hook_init() function.

drupal_get_filename('module', 'user', 'modules/user/user.module');
drupal_load('module', 'user');

kingandy’s picture

drupal_add_js() has an "every_page" option which makes it possible to add javascript in hook_init, as long as you're adding a static piece of script or file which is exactly the same on every page view (so it still functions correctly if it's cached and/or aggregated):

function mymodule_init() {
  $code = [your javascript code]
  drupal_add_js($code, array(
    'type' => 'inline',
    'scope' => 'footer',
    'every_page' => TRUE,

A .info file "scripts[]" entry is probably still the preferred method for adding internal files, but this can be useful if you have a desperate need to leverage some of the other drupal_add_js options like 'scope' or 'group'.