How to get the base url of a specific store view in a static block?

How to get the base url of a specific store view in a static block?

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get the base url of a specific store view in a static block?

Getting the Base URL of a Specific Store View in a Magento Static Block

In Magento, accessing store-specific URLs or base URLs is a common requirement for both PHTML files and static block content. While fetching the base URL of a specific store view within a PHTML file is straightforward using the helper class, doing so directly inside a static block requires a slightly different approach.

Understanding Magento’s Static Block Structure

Static blocks in Magento are used to store reusable content that can be placed on multiple pages or layouts. They are managed through the admin panel under Content -> Widgets -> Blocks. The content of these blocks is stored as plain text without any PHP code execution capability by default.

Fetching Store-Specific URLs Inside a Static Block

To fetch the base URL of a specific store view within a static block, you cannot directly use PHP or helper classes like in PHTML files. Instead, you can leverage Magento’s template processing and include the necessary code snippets that will be executed during the layout rendering process.

Here’s how to achieve this:

  1. First, ensure your theme directory is set up correctly for template modification. If not, copy the block/static/blocks folder from the base theme (e.g., app/design/frontend/base/default) to your custom theme directory (e.g., app/design/frontend/Vendor/theme).

  2. Open or create a new file named phtml.phtml inside the templates subfolder of your static block’s template path, e.g., block/static/blocks/mystaticblock/templates.

  3. Add the following code snippet to the phtml.phtml file:

<?php echo $this->getUrl('dcmotosesp', array('_secure'=>true)) ?>
  1. Save and close the file.

Explaining the Code Snippet

The $this->getUrl() method is a built-in Magento function that generates a URL based on the given parameters. In this case, ‘dcmotosesp’ refers to the store view code, and the array _secure=>true ensures the URL is generated with an SSL prefix if the current request is secure.

When rendering the page layout, Magento will process any included template files like phtml.phtml. During this processing, the PHP code inside the file gets executed, allowing you to fetch the desired store-specific base URL.

Alternative Approach: Using Layout XML

If you prefer not to modify theme templates directly or want a more flexible solution that can be applied across multiple static blocks, you can use Magento’s layout XML configuration.

  1. Open your custom theme’s layout folder (e.g., app/design/frontend/Vendor/theme/layout).

  2. Create a new file named mystaticblock.xml.

  3. Add the following code to the mystaticblock.xml file:

<layout version="0.1.0">
    <block type="core/template" name="my.store.url.block" template="blocks/mystaticblock/phtml.phtml"/>
</layout>

Replace mystaticblock with the actual code of your static block.

  1. Save and close the file.

This approach creates a new template block that is linked to your static block content. The layout XML configuration enables you to specify the desired template, allowing Magento to process the PHP code during page rendering without directly modifying core templates.

Conclusion

Fetching the base URL of a specific store view within a Magento static block can be accomplished by leveraging either theme template modification or layout XML configuration. Both methods allow you to include PHP logic that gets executed during page processing, enabling you to access store-specific URLs in your static block content.

Remember to test your setup thoroughly and ensure compatibility with any updates or changes to the Magento core. For more advanced customization options, consider exploring Laravel Company’s Magento development expertise for professional assistance tailored to your specific needs.

Happy coding!

Themes Admin

Our network