44

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?

7ochem
  • 2,183
  • 1
  • 34
  • 42
datasn.io
  • 12,564
  • 28
  • 113
  • 154
  • 1
    Please do not put PHP function code to the theme. This should be in the module (`app/code/X/...`) – Alex Oct 08 '14 at 12:04

6 Answers6

107

The way that Magento themes handle actual url's is as such (in view partials - phtml files):

echo $this->getSkinUrl('images/logo.png');

If you need the actual base path on disk to the image directory use:

echo Mage::getBaseDir('skin');

Some more base directory types are available in this great blog post:

http://alanstorm.com/magento_base_directories

philwinkle
  • 7,036
  • 3
  • 27
  • 46
17

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:

  • _area frontend (default) or adminhtml
  • _package your package
  • _theme your theme
  • _relative when this is set (as an key) path relative to Mage::getBaseDir('skin') is returned.

So in your case correct answer would be:

require(Mage::getDesign()->getSkinBaseDir().DS.'myfunc.php');
Deus777
  • 1,816
  • 1
  • 20
  • 18
13

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.

Nikhil_K_R
  • 2,383
  • 5
  • 27
  • 51
  • What does it adds to the currently accepted answer? This is exactly the same suggested there. This should be a comment, not an answer. Check this [metaSO question](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) and [Jon Skeet: Coding Blog](http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx) on how to give a correct answer. – Yaroslav Oct 11 '12 at 18:25
8

To get current skin URL use this Mage::getDesign()->getSkinUrl()

Justin
  • 26,443
  • 16
  • 111
  • 128
Jitendra Mandloi
  • 283
  • 2
  • 12
  • 3
    You can pass the path as a parameter `Mage::getDesign()->getSkinUrl('image/example.png')` – Justin May 15 '15 at 20:23
1

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();
-1

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.

Sankar Subburaj
  • 4,992
  • 12
  • 48
  • 79
  • Any modifications made in app/code/core will be overwritten upon Magento upgrade, and it is therefore inadvisable to make any changes within that hierarchy. – Aaron Miller Oct 08 '12 at 20:42