Magento wysiwyg images don’t work when using a symlink

I recently started deploying a magento project using capistrano, which symlinked the public_html directory to a generated subdirectory of “releases”. After some digging I found Magento uses dirname(__FILE__) and the realpath() function quite a bit, which resolves symlinks, which causes Magento to use file paths that point at the symlinked directory instead of public_html. This mostly works, but there are a few bugs out there including the one I encoutered where when trying to insert an image from the wysiwyg editor the image would not be found. The solution is to extend Mage_Cms_Helper_Wysiwyg_Images with your own custom module and override the getCurrentUrl() method with the below. You only have to change one line.

public function getCurrentUrl()
		if (!$this->_currentUrl) {
			//$path = str_replace(Mage::getConfig()->getOptions()->getMediaDir(), '', $this->getCurrentPath());
			$path = str_replace(realpath(Mage::getConfig()->getOptions()->getMediaDir()), '', $this->getCurrentPath());
			$path = trim($path, DS);
			$this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') .
			$this->convertPathToUrl($path) . '/';
		return $this->_currentUrl;

Leave a comment

Your email address will not be published. Required fields are marked *