Drupal is a registered trademark of Dries Buytaert
drupal 11.3.8 Update released for Drupal core (11.3.8)! drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! linkit 7.0.14 Minor update available for module linkit (7.0.14). masquerade 8.x-2.2 Minor update available for module masquerade (8.x-2.2). video_embed_field 3.1.0 Minor update available for module video_embed_field (3.1.0). bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). editoria11y 3.0.0 Major update available for module editoria11y (3.0.0). trash 3.0.27 Minor update available for module trash (3.0.27). twig_tweak 4.0.0-alpha2 New alpha version released for module twig_tweak (4.0.0-alpha2). twig_tweak 4.0.0-alpha1 First alpha version released for module twig_tweak (4.0.0-alpha1). node_revision_delete 2.1.1 Minor update available for module node_revision_delete (2.1.1). commerce_paypal 2.1.2 Minor update available for module commerce_paypal (2.1.2).

add_to_head

7,222 sites Security covered
View on drupal.org

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

Activity

Total releases
3
First release
Mar 2026
Latest release
2 weeks ago
Release cadence
18 days
Stability
0% stable

Release Timeline

Releases

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