How to include .php file in snippet/templatefile

Hi there!
I’m trying to implement my first website with kirby and i’m totally happy with the system.
Now i have a probably very trivial problem … how can i include a php-file into a template file or a snippet?

include_once(url('/site/functions/func.helpers.php'));

… unfortunately does not work.

Error message:

include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0

Unfortunately I can’t get behind it with documentation and google!

I would be very happy if someone can help me here.

Greetings Robert

The site folder is not accessible via URL, use the path to the file instead:

require_once($kirby->root('site') . '/functions/func.helpers.php');

Alternatively, put your function file into a plugin folder (e.g. /site/plugins/helpers/index.php) and rename to index.php, then it will be auto loaded.

3 Likes

Thank you. Works perfect!

Putting everything in the plugins folder is great! It can seem a little odd at first, but then you’ve created your first Kirby plugin!

1 Like

Thanks for the input with the plugin.

Since the solution

require_once($kirby->root('site') . '/functions/func.helpers.php');

did not work as desired I’ trying the solution with “…/helpers/index.php.”

A simple “echo” eg. echo “test” works fine …that shows me that the index.php is loaded from the plugin

However I cannot call any function from the plugin in a template file …

When I try to call a test() function I get the following message:

Error: Call to undefined function test ()

What I’m doing wrong?

Robert

So the index.php is /site/plugins/helpers/index.php, right?

Could you post an example of what you have in there and how you are trying to use it?

Hi Sonja,

here is an example of whats in the /site/plugins/helpers/index.php

function camelCase($string,  $capitalizeFirstCharacter = false) {
   $search = array(' ', '-','_');
   $string = str_replace($search, '', ucwords($string, implode('', $search)));
   if (!$capitalizeFirstCharacter) {
      $string = lcfirst($string);
   }
   return replaceUmlaut($string);
}

And here is an example of how the function in my template file is called:

...
<body id="<?=camelCase($page->intendedTemplate())?>">
...

Thanks,
Robert

And the error is that the camelCase() function is not found? And the replaceUmlaut() function exists as well? And your file starts with a <?php tag?

I am so sorry.

That was my fault…

I still had a <? php namespace on the first line I’ve overlooked. I’m still very busy learning kirby at the moment … but I’m totally thrilled :slight_smile:

I’d better take a closer look at what I do before I post here…

Thanks for your super fast support!
Robert

No worries!