Do you want to code more quickly and easily in Joomla? Are you looking for some reliable code snippets that you can quickly insert and customize to fit your Joomla code? Let’s get to know some useful Joomla code snippets that will help you to get better at coding.

A code snippet is a small reusable code. These are some commonly used code blocks while coding a template or extension. When you design your own template in Joomla, you can use the PHP code snippets to extend your website. Joomla code snippets aid you to perform simple tasks like adding script files, module positions, image, links and much more. These are very helpful for a beginner.

How to Use the Joomla Code Snippets?

Basically, you will add a Joomla code snippet to the index.php file if you want it to apply to all pages of a template. Such as adding a javascript, CSS file to the template will be mentioned in an index.php file. Otherwise, you will add a Joomla code snippet to the mypage.php. Here, mypage can be any page of the template where you want to reflect the changes. you can also read about famous Joomla hosting services.

Code Snippets for Joomla

Here at TemplateToaster offline website builder software showcases the list of some useful code snippets for Joomla that you may use as per your requirement. You may also dowmload free Joomla templates.

1. Add js file to Head of the Template

This Joomla snippet will add any specified javascript file to the head of the template. It is necessary for proper functioning of your template. You can add this snippet to module or plugins also.

$host = JURI::root();
$document =& JFactory::getDocument();
$document->addScript($host.'path/jquery-1.4.4.min.js');

Source

2. Add CSS to a Template

The CSS files contain all the information about styling a particular element of your site. So, you will add CSS to the header section. This Joomla snippet can also be used for module or plugins.

First of all, get a reference to the current object

$document = JFactory::getDocument();

Then, add CSS to it

global $template_path;
$template_path = JURI::base() . 'templates/' . $app->getTemplate();
$document->addStyleSheet($template_path.'/css/template.css');

3. Add a Module Position

A module position is a placeholder in your template. It tells about different positions of your template. A programmer has full control over module positions. This Joomla code snippet is used to add a module position in your template.

<body>
<jdoc:include type="modules" name="top" />
<jdoc:include type="modules" name="bottom" />
</body>

Here, two modules are added to the body of the page named – top, bottom. For more details, you can read the article to add a custom Joomla module position.

Source

Note: You can add module lines anywhere in the body. But you have to add a corresponding line to the templateDetails.xml file which sits alongside the index.php.

4. Remove Default Script Libraries

You will add this Joomla code snippet in Joomla 1.7+ before <jdoc:include type=”head” />. It will prevent loading of default scripts.

<?php
$this->_scripts = array();
?>

Source

5. Remove Default scripts except for MyComponent Pages

This Joomla code snippet helps to load the libraries for component pages.

<?php if (JRequest::getVar('option')!=='com_mycomponent') {
$this->_scripts = array();
} ?>

Source

6. Refer Google Hosted jQuery Library

You can add this Joomla code snippet to refer a jQuery library of Joomla rather than downloading it to your website.

$document = &JFactory::getDocument();
$document->addScript( "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" );

Source

7. Remove Breadcrumbs from the Frontpage

You can use these Joomla code snippets in Joomla to remove breadcrumbs in a monolingual or multilingual site.

For Monolingual Site

<?php if ($menu->getActive() !== $menu->getDefault()) { ?>
<?php if($this->countModules('breadcrumbs')) : ?>
<jdoc:include type="modules" name="breadcrumbs" style="none" />
<?php endif; ?>
<?php } ?>

For Multilingual Site

<?php if ($menu->getActive() !== $menu->getDefault( 'en-GB' ) && $menu->getActive() !== $menu->getDefault( 'sl-SI' )) { ?>
<?php if($this->countModules('breadcrumbs')) : ?>
<jdoc:include type="modules" name="breadcrumbs" style="none" />
<?php endif; ?>
<?php } ?>

Source

8. Edit Page Title

If you want to set a new title for any particular page, you can use this Joomla code snippet.

$document = JFactory::getDocument();
$document->setTitle(‘Set your Title here’);

Source

Code Snippets for generic use on Any Page

9. Add an Image

You can add an image to the template using this Joomla snippet. You will get the source image from “images” subfolder in “Template” folder…

<img src="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/images/myimage.png" alt="Joomla Code Snippets" class="customImage" />

Source

10. Get The Active Menu Item

This Joomla snippet is used to get the active menu item.

$menu = &JSite::getMenu();
$active = $menu->getActive();
$menuName = $active->name;
$menuLink = $active->link;

Source

11. Insert the Name of the Site

This code will display the name of the site from the site variable.

<?php echo $app->getCfg('sitename'); ?>

Source

12. Insert Link to the Homepage

While designing a site, you frequently set a link to the homepage. For this, you can add this code snippet.

<?php echo $this->baseurl ?>

Source

13. Get Component Name

It will get the name of Joomla Template component.

$comp_name = JFactory::getApplication()->input->get('option');

Source

14. Get View Name from Joomla

This Joomla code snippet will get the Template name as specified in index.php file.

<$view_name = JFactory::getApplication()->input->get('view');

Source

15. Get Article id from Joomla

If you want to fetch article-id from index.php file of the template, you can use this Joomla code snippet.

$article_id = JFactory::getApplication()->input->get('id');

Source

16. Get SEF Prefix in a Multilanguage Site

This Joomla code snippet is used to add SEF language prefix to all the URLs.

jimport('joomla.language.helper');
$languages = JLanguageHelper::getLanguages('lang_code');
$lang_code = JFactory::getLanguage()->getTag();
$sef = $languages[$lang_code]->sef;

Source

17. Detect a Given Component

This Joomla code snippet is to detect com_component pages in your template.

<?php if (JRequest::getVar('option')=='com_component') { ?>
//HTML code that will show only on the com_component components page
<?php } ?>

Source

18. Detect a Given View

This Joomla code snippet will display the view of category page only.

<?php if (JRequest::getVar('view')==’category’) { ?>
//HTML code that will show only on the category page
<?php } ?>

Source

19. Detect a Given View & ID

It will show the view on a category page and have the specific id.

<?php if (JRequest::getVar('view')=='categories' && JRequest::getVar('id')==1) { ?>
//HTML code that will show only on the categories page with the id=1
<?php } ?>

Source

20. Get Current URL

This Joomla code snippet will fetch the current absolute URL of the page.

$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));

Source

21. Get Article by Article_id

If you want to get an article by article_id, you can use this Joomla code snippet. Here, “$articleId” specifies the ID of that article from which you want to search code. If the article doesn’t exist, it will display that the article is empty.

$db = &JFactory::getDBO();
$sql = "SELECT introtext FROM #__content WHERE id = ".intval($articleId);
$db->setQuery($sql);
$fullArticle = $db->loadResult();
if(!strlen(trim($fullArticle)))
$fullArticle = "Article is empty ";

Source

22. Render Module from Template

To embed a module in Joomla, you can use this code snippet.

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_related_items');
$attribs['style'] = 'xhtml';
echo JModuleHelper::renderModule( $module, $attribs );

Source

23. Get Active User-Id

Use this Joomla code snippet to get the id of the currently active user.

$user =& JFactory::getUser();
$user_id = $user->id;

Source

24. Get Global Variable

You will use this Joomla code snippet to get the Joomla global variable.

$app = &JFactory::getApplication();
$params = $app->getParams();

Source

25. Detect Whether in Front Page

This Joomla code snippet is used to check whether the current URL is an instance of the front page.

$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));
if($myabsoluteurl==JURI::base(true).'/')
$front_page = TRUE;

Source

 Joomla code snippets – Wrapping Up

These Joomla code snippets are very useful for beginners as well as intermediate-level programmers. They need to perform these small tasks frequently. So, they can easily embed these snippets as required. As small change keeps us moving and can give better results. Hopefully, you like this article. Please share your views in the comment section below.