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.

This hook is run at the beginning of the page request. It is typically used to set up global parameters which are needed later in the request.

Only use this hook if your code must run even for cached page views. If you have code which must run once on all non cached pages, use hook_menu(!$may_cache) instead. Thats the usual case. If you implement this hook and see an error like 'Call to undefined function', it is likely that you are depending on the presence of a module which has not been loaded yet. It is not loaded because Drupal is still in bootstrap mode. The usual fix is to move your code to hook_menu(!$may_cache).

Return value


Related topics

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

conf_init in includes/bootstrap.inc
Loads the configuration and sets the base URL, cookie domain, and session name correctly.
phptemplate_init in themes/engines/phptemplate/phptemplate.engine
@file Handles integration of templates written in pure php with the Drupal theme system.
tablesort_init in includes/tablesort.inc
Initialize the table sort context.
variable_init in includes/bootstrap.inc
Load the persistent variable table.
2 invocations of hook_init()


developer/hooks/core.php, line 538
These are the hooks that are invoked by the Drupal core.


function hook_init() {
  global $recent_activity;
  if (variable_get('statistics_enable_auto_throttle', 0) && !rand(0, variable_get('statistics_probability_limiter', 9))) {
    $throttle = throttle_status();

    // if we're at throttle level 5, we don't do anything
    if ($throttle < 5) {
      $multiplier = variable_get('statistics_throttle_multiplier', 60);

      // count all hits in past sixty seconds
      $result = db_query('SELECT COUNT(timestamp) AS hits FROM
        {accesslog} WHERE timestamp >= %d', time() - 60);
      $recent_activity = db_fetch_array($result);


Heine’s picture

As with many other hooks, adding an implementation of hook_init() to an existing module might give you inconsistent behaviour due to the various caches Drupal employs. As a symptom you'll notice that your hook_init() implementation doesn't run on cached pages. Visit admin/build/modules to remedy.

The cause: A module is only loaded during bootstrap on cached pages if its "bootstrap" flag in the system table is set. This flag is set when module_rebuild_cache runs.

So, if you add a hook_init() implementation to a module Drupal already knows about, the flag is not set (and thus the hook_init() implementation not executed on cached pages) until you visit admin/build/modules.