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'] = array(
'name' => 'facebook-remarketing-pixel',
'paths' => '<front>',
'scope' => 'scripts',
'path_visibility' => 1,
'roles' => array(
'visibility' => 0,
'list' => array(
2 => 2,
3 => 3,
1 => 0,
6 => 0,
4 => 0,
5 => 0,
11 => 0,
16 => 0,
),
),
'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&ev=PixelInitialized" /></noscript>
CODE
,
);
}
Similar modules
Project information
- Module categories: Administration Tools, Content Editing Experience, Content Display
- 5,374 sites report using this module
- Created by nicholasThompson on , updated
- Drupal 10 is here!
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
Releases
Drupal 10 compatibility
Development version: 8.x-1.x-dev updated 5 Jan 2023 at 16:07 UTC
Development version: 7.x-1.x-dev updated 4 Feb 2016 at 22:38 UTC