Get skin path in Magento?

115,529

Solution 1

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

Solution 2

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');

Solution 3

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.

Solution 4

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

Solution 5

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();
Share:
115,529

Related videos on Youtube

datasn.io
Author by

datasn.io

Not funny at all

Updated on July 09, 2022

Comments

  • datasn.io
    datasn.io almost 2 years

    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?

    • Alex
      Alex over 9 years
      Please do not put PHP function code to the theme. This should be in the module (app/code/X/...)
  • Aaron Miller
    Aaron Miller over 11 years
    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.
  • Yaroslav
    Yaroslav over 11 years
    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 and Jon Skeet: Coding Blog on how to give a correct answer.
  • Justin
    Justin almost 9 years
    You can pass the path as a parameter Mage::getDesign()->getSkinUrl('image/example.png')