Code In Database

admin's picture

Avoid including code in your database.

Common places code is included:

  • Pages
  • Blocks
  • Contentplate
Why?
  • Revision control becomes impossible
  • Difficult to document
  • Difficult to debug
  • Hard to move between systems
  • Difficult to upgrade
How To Avoid
  • Add your code into a new module
  • Add your code to the theme template.php file

 

Comments

keithhurst's picture

Code in template.php

Hi,

this is a fine idea moving code from the page to template php so I thought I would give it a try on one of my pages.

I created a new function foobar in my template.php and in there constructed an $output string. The final line of the function is return $output;.

In my new test page I created a very simple php script

<?php
print foobar() ;
?>

When I preview the page all is fine but when I submit the page I get...

"Fatal error: Call to undefined function foobar() in /homepages/24/d236848111/htdocs/hrfc/includes/common.inc(1352) : eval()'d code on line 2."

What am I doing wrong?

Thanks
Keith

[EDIT] - Solved it, I needed to put a call to init_theme before the call to my own function.