Using the code snippet lets understand how to get Media URL in Magento 2.

For Getting Media URL Use Below code:

$mediaUrl =$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Using the following method, you can get media URL in your template file, but without using object manager, you need to define Block file using the method construct() to define store Manager Interface in the method of construct.

In your phtml Block file create __construct function.

Add to __construct in your class

protected $_storeManager;

public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)


       $this->_storeManager = $storeManager;


Add new method in your class

public function getMediaUrl($path)


        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $path;


Call in phtml

<img src=”<?php echo $block->getMediaUrl(‘wysiwyg/img.jpg’) ?>” />

Previous Next