Community Documentation

hook_init

5 core.php hook_init()
6 core.php hook_init()
7 system.api.php hook_init()
8 system.api.php hook_init()

Perform setup tasks. See also, hook_boot.

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. when this hook is called, all modules are already loaded in memory.

For example, this hook is a typical place for modules to add CSS or JS that should be present on every page. This hook is not run on cached pages - though CSS or JS added this way will be present on a cached page.

Return value

None.

Related topics

▾ 15 functions implement hook_init()

aggregator_init in modules/aggregator/aggregator.module
Implementation of hook_init().
blogapi_init in modules/blogapi/blogapi.module
book_init in modules/book/book.module
Implementation of hook_init(). Add's the book module's CSS.
conf_init in includes/bootstrap.inc
Loads the configuration and sets the base URL, cookie domain, and session name correctly.
dblog_init in modules/dblog/dblog.module
forum_init in modules/forum/forum.module
Implementation of hook_init().
lock_init in includes/lock-install.inc
Initialize the locking system.
lock_init in includes/lock.inc
Initialize the locking system.
node_init in modules/node/node.module
Implementation of hook_init().
phptemplate_init in themes/engines/phptemplate/phptemplate.engine
Implementation of hook_init().
poll_init in modules/poll/poll.module
Implementation of hook_init().
system_init in modules/system/system.module
Implementation of hook_init().
tablesort_init in includes/tablesort.inc
Initialize the table sort context.
user_init in modules/user/user.module
Implementation of hook_init().
variable_init in includes/bootstrap.inc
Load the persistent variable table.

File

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

Code

<?php
function hook_init() {
  drupal_add_css(drupal_get_path('module', 'book') . '/book.css');
}
?>

Comments

404 pages

You can of course use hook_init for any page not being served from cache. This includes 404 pages. http://drupal.org/node/468494#comment-1630520

And what if I need a check to

And what if I need a check to be performed on a cached pages?

page_cache_fastpath looks

ok, noticed hook_boot, will try it

Anonymous user run a page(views page) hook_init not work

I create new module have my_module_init function. Anonymous run a page (page created by views module )my_module_init not working.

hook_init only working for logged-in users?

Now working

After Disable Page cache now working good.

Login or register to post comments