Question

I need to reference a particular functions.php file, that can be stored in the template directory of my Joomla template. WordPress seems to include this, but if I do something like:

Include the functions file

<?php require_once ( 'functions.php' ); ?>

In the functions.php:

<?php
     function displaySomething() {
          echo "Hello World";
 }
?>

and then in the templates index.php:

<?php displaySomething(); ?>

The template breaks/is blank. For aguements sake, I just want to have a single referencable php case function.

Was it helpful?

Solution

Try this -

<?php require_once ($this->baseurl.'/templates/'.$this->template.'/functions.php'); ?>

However, chances are that you shouldn't do it this way. In general it's not a good idea to hard code anything in to your template because that removes the ability to select the pages that you want the code to be executed on and it removes the ability to easily edit what ever the code is doing.

You really should put your code in a module or plugin. Putting it in a module makes it easy to select the pages you want the code on, turn it on or off globally, or to change the output easily. Putting it in a plugin would make it easy to put it on pages based on one of the Joomla event triggers, and to turn it on or off globally.

For the amount of time it would take to turn it in to an extension, it is well worth it.

OTHER TIPS

Try with (using the constant JPATH_BASE):

<?php
 #Loading functions
 require_once (
  JPATH_BASE."/templates/{$this->template}/functions.php"
 );
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top