Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Sodium 3.0.2 Minor update available for module sodium (3.0.2). Release: Snipper 1.0.4 Minor update available for module snipper (1.0.4). Release: X-Frame-Options Allow-From webvisor.com 2.0.2 Minor update available for module xframe_allow_webvisor (2.0.2). Release: Swagger UI Info 2.0.2 Minor update available for module swagger_ui_info (2.0.2). Release: Contact Mail 2.0.4 Minor update available for module contact_mail (2.0.4). Release: Varbase Webform Base 1.0.1 Minor update available for module varbase_webform_base (1.0.1). Release: BootBase 2.0.3 Minor update available for theme bootbase (2.0.3). Release: URL Rewriter 1.0.0-alpha4 New alpha version released for module url_rewriter (1.0.0-alpha4). Usage Milestone: Term CSV Export Import Module term_csv_export_import crossed 1,000 active installs. Module Revived: Year/Month Widget 1.1.3 Module year_month_widget updated after 9 months of inactivity (1.1.3).

Add To Head

6,942 sites Security covered
View on drupal.org

This module allows you to inject custom code, scripts, or styles into specific parts of your page's HTML head or before the closing body tag. You can control where and for which users this content appears, making it useful for adding tracking snippets or custom JavaScript.

This project allows you to inject content into the $head, $scripts and $styles elements in the page.tpl.php. This is defined in profiles which can target URLS's.

Note: Only users with the administer add to head permission can use this and, as they can inject whatever they want, this permission should only be granted to VERY trusted users and site administrators as it could be used for evil.

This module is particularly useful if you need to include a specific JavaScript snippet on SPECIFIC pages. You could do this using a block, however some JavaScript requires that it is in the <head> region of the page template.

Features

  • Select paths for inclusion or exclusion.
  • Select roles for inclusion or exclusion.
  • Choose the scope of addition.
    • Head: This appears early on in the head (before any CSS and JS are included).
    • Styles: It will be appended to the CSS files section. This is usually before any other JS is included.
    • Scripts: It will be appended to the JavaScripts section. This can, sometimes, be in the footer of the document depending on the theme.
  • Code snippet profile information (metadata and the code itself) can be provided via code through a hook_add_to_head_profiles_alter() implementation in your custom modules. This is the recommended approach as code should not be stored in the database for security reasons. See below for an example.

Specifying code snippet profile information via code (recommended)

Here is an example that adds the Facebook remarketing/retargetting pixel. The code itself is set using nowdoc syntax (available since PHP 5.3.0).

/**
 * Implements hook_add_to_head_profiles_alter().
 */
function MYMODULE_add_to_head_profiles_alter(&$profiles) {
  // Add the Facebook remarketing pixel.
  $profiles['facebook-remarketing-pixel'] = [
    'name' => 'facebook-remarketing-pixel',
    'scope' => 'scripts',
    'paths' => [
      'visibility' => 'include',
      'paths' => '<front>',
    ],
    'roles' => [
      'visibility' => 'exclude',
      'list' => [],
    ],
    'code' => <<<'CODE'
<!-- start of Facebook pixel tag-->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
_fbq.push(['addPixelId', 'XXXXXXXXXXXXXXX']);
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', 'PixelInitialized', {}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXXXXXXX&amp;ev=PixelInitialized" /></noscript>
CODE
  ];
}

Similar modules

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Mar 2026
Latest release
4 months ago
Releases (12 mo)
3 ▲ from 0
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
2.x-dev Dev 8–11 Apr 13, 2026
8.x-1.0-beta3 Pre-release 8–11 Mar 25, 2026
8.x-1.0-beta2 Pre-release 8–11 Mar 9, 2026