Modifying the Head Section In Your Joomla Page

There are three ways you can modify what is written to your head section.

  1. You can modify directly the /libraries/joomla/document/html/renderer/head.php in the fetchHead() function. (Not Recommended)
  2. Use the JDocument methods to insert meta and script tags in the head section.
  3. Hard code the meta and script tags directly in the template index.php file.

Try not to do the first option at all costs. When it comes to upgrade you might have to move all your changes to the new head.php or lose your changes if you forget.

This article is about the second option. It is the best way to modify the tags in the head section. Your not messing with core files and you can add to the head section from any template, component or module file.

You first need to get an instance of the current document and call the methods to modify the head. The head section tags will be added before the page is rendered.


//First get the current document object
$doc = &JFactory::getDocument();

//Will create a empty generator meta tag. If you dont want everyone to know site is running on Joomla.
$doc->setGenerator();
//Sets the description meta tag
$doc->setDescription('some desc');
//sets the title tag
$doc->setTitle('Some Title');
//create a meta tag
$doc->setMetaData($name,$content);
//adds a linked style sheet
$doc->addStyleSheet('/path/to/file')
//adds a linked javascript or other type of script file
$doc->addScript('/path/to/file')
//add a custom tag. Use to add any kind of tag to the head section.
$doc->addCustomTag();
//Add custom javascript code snippet. Pass in javascript code and will add the <script> tags for you. Joomla places these snippets after placement of addScript() scripts.
$doc->addScriptDeclaration()
//Add custom css style snippet. Pass in css styles and will add the <style> tags for you. Joomla will place these styles after placement of addStyleSheet() styles.
$doc->addStyleDeclaration()

Can also include external javascript and css files in the head using JHTML object


//This will create a script tag in the head loading a jquery file located in script/js/ folder.
JHTML::script('jquery-1.4.2.min.js','script/js/');

//This will create a link tag in the head loading a style sheet.
JHTML::stylesheet('template.css', 'script/css/');

Here is the api document for JDocument and this is the doc for jDocumentHTML which inherits from JDocument and used to generate the normal html output.

Leave a comment

6 Comments.

  1. Very good info, i was searching for this from last week, saved a lot of time, thank you

  2. Thanks! i was going crazy looking for something like this, this is exactly what i needed. Two thumbs way up!

  3. This is what I need, but I’m new at Joomla. Where do I put this code? And maybe how, what menu should I go through?
    Thanks

  4. You can place this code just about anywhere in the controller, model, view or the template index.php.

  5. Still not clear where these stuff should be placed!

  6. You can place this code in the controller display() method.

    You can put this code in any view view.html.php file in the display() method.

    You can place this code in a template override. e.g. /templates/sometemplate/html/com_mycom/someview/default.php

    Those are the places I put it in.

Leave a Reply


[ Ctrl + Enter ]