By 2ge on
Hello,
I am beginner in Drupal, I'd like to call some custom PHP functions from content. Where is the best place for them? I thought make a module ('custom.module'), enable it in modules section, write some functions there and call them from content. But I don't know, if this is a standard way?
Thanks guys!
Comments
Write page with php code inside
See my title, I think this is what you want...
Well, can you explain a little what you really want ?
I didn't understand " I'd like to call some custom PHP functions from content", do you want to display a php date inside a page ???
Can you define PHP functions?
Is it possible to define PHP snippets somewhere outside of the node such that you could execute the same code on several nodes?
e.g. There's one snippet that displays the last n nodes of a certain taxonomy term. Is there somewhere that you could you create a function lastn and then simply call it from within you page?
So, you'd define the function like this:
Then on every node you wanted to execute the code all you'd need to do would be something like this:
Thanks.
it is easy
no, I have to declare my own functions, they are not simple as calling date function, I will have some ini values, and based on those content will be generated from some external files (html, pictures, flash...).
So I need one general own function and call it from PAGE content.
I would suggest putting your
I would suggest putting your code in a separate .inc file and including it from your settings.php file. The default settings file is in sites/default/settings.php. If you created the file sites/default/myfunctions.php, you could included it from settings.php as:
This assumes that this directory is in PHP's include_path.
This module is exactly what
This module is exactly what you're looking for...
Pobster
Thanks!
Will try this, I'm using include in as wrote javanaut, but this sounds to me more clear. Also thanks for nice reply - will try this for sure!
Just one question: when I am on root category (home), I have list of nodes (excerptions). How to display now to each node its own NID ? With this I'm able to call own function, what I need.
Thanks.
Be careful with just using
Be careful with just using include. If you have 2 nodes that use the same function then viewing each separately will be OK, but anytime you view the two together (eg via taxonomy) or with cron.php you may get a redeclare error. Use include_once instead.
OK
Ok, I changed that to include_once, but I guess "sites/default/settings.php" is always including only one time, and I'm including from this file.
including custom functions from settings file
how can I include it from settings.php file?
Putting in settings.php a simple line such as "include ('my_custom_functions.php')" doesn't work :-(
You might need to read the php docs
The include() function docs are here:
http://www.php.net/include/
Those docs reference the include_path which is documented here:
http://www.php.net/manual/en/ini.core.php#ini.include-path
I hope this helps
Also, check out ini_set
setting the include_path setting can be done with the following code:
You'll want to make sure to include the directory containing your inc file.
re: "You might need to read the php docs"
thank you for your answer but what has the php manual to do with my question? All I needed was a short - as short as possible - example of the including line in 'settings.php'... Shall I put an 'include...' statement somewhere in that file or what?
Unfortunately I have no access to the php.ini file to the place where I host my Drupal site so I cannot mess around with it...
Till I find the mysterious way of using 'settings.php' I just pasted my humble custom functions in a Drupal core file and it works (till now :->).
Thanks anyway for your answers
This works 4 me
Dunno if anyone solved the problem of includes and requires.
I have Drupal 6.12 installed. What I put into settings.php is simply "require_once('file.php')" and then create the "file.php" file in root folder where Drupal is installed. It gets picked up and works like a charm.
Of course, you can create a this file under any folder within Drupal folder structure and point the requiring function there.
Hope this helps.
subscribing to thread
subscribing to thread
Did you figure it out?
I'm having the same problem and I'd like to know if you ever figure out where to put custom functions in other place other than the core inc files.
Simple approach is make a
Simple approach is make a module, you will need the .module and .info files. You can then add functions to the .module file that can be called from elsewhere. To avoid name clashes your function names should start with your module name.
Simple approach that works!
I wanted to create a custom pager for my site, so the first thing that I did was to create a simple module to hold my custom function. I followed the instructions here http://drupal.org/node/416986
Then I put my pager function in the .module file and now this function can be called from any other module if I need to.
Since I'm new to Drupal it was a good exercise to learn more a little bit about this great software.
You can see the pager in action at the bottom of my home page: http://www.f1community.org/
In case you are wonder why I wanted to create a custom pager, it is because the core pager.inc make use of the theme_item_list that is based on (li) tags and when you style the list it would affect any other item_list around the site.
Thanks for the tip.
Another solution would be
Another solution would be to override theme_pager().
You can do that, but .....
Yes, you can do that, as a matter of fact I tried it but did not work the way I wanted, because the override function would be use for every module that uses the ul li list.
I wanted my custom pager to be use for one module only.
The best option is to create your module to hold your functions, so you have more control over it.
Custom functions in drupal 7
Not sure about previous versions, but in 7 you can just place your custom functions directly in template.php in your theme folder. just make sure not to overwrite any core functions.