I have a few custom PHP functions for my Magento store that I stored in myfunc.php and I need to require it from in a few different .phtml files. How do I do that?
I mean I can use an absolute path but that would be dirty and probably problematic when migrating to another server.
For now I'm stuck with:
require('/home/myuser/public_html/app/design/frontend/default/mytheme/myfunc.php');
How do I refer to the skin path ( /home/myuser/public_html/app/design/frontend/default/mytheme/ ) programmatically?
To get current skin URL use this Mage::getDesign()->getSkinUrl()
First of all it is not recommended to have php files with functions in design folder. You should create a new module or extend (copy from core to local a helper and add function onto that class) and do not change files from app/code/core.
To answer to your question you can use:
require(Mage::getBaseDir('design').'/frontend/default/mytheme/myfunc.php');
Best practice (as a start) will be to create in /app/code/local/Mage/Core/Helper/Extra.php a php file:
<?php
class Mage_Core_Helper_Extra extends Mage_Core_Helper_Abstract
{
public function getSomething()
{
return 'Someting';
}
}
And to use it in phtml files use:
$this->helper('core/extra')->getSomething();
Or in all the places:
Mage::helper('core/extra')->getSomething();
To use it in phtml apply :
echo $this->getSkinUrl('your_image_folder_under_skin/image_name.png');
To use skin path in cms page :
<img style="width: 715px; height: 266px;" src="{{skin url=images/banner1.jpg}}" alt="title" />
This part====> {{skin url=images/banner1.jpg}}
I hope this will help you.
To get that file use the below code.
include(Mage::getBaseDir('skin').'myfunc.php');
But it is not a correct way. To add your custom functions you can use the below file.
app/code/core/Mage/core/functions.php
Kindly avoid to use the PHP function under skin dir.
First note that
Mage::getBaseDir('skin')
returns only path to skin directory of your Magento install (/your/magento/dir/skin).
You can access absolute path to currently used skin directory using:
Mage::getDesign()->getSkinBaseDir()
This method accepts an associative array as optional parameter to modify result.
Following keys are recognized:
So in your case correct answer would be:
require(Mage::getDesign()->getSkinBaseDir().DS.'myfunc.php');
Source: Stackoverflow.com