In Magento 2 How to Override phtml or layout core files?

In Magento 2 How to Override phtml or layout core files?

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
In Magento 2 How to Override phtml or layout core files?

Overriding Core Files in Magento 2: A Comprehensive Guide

As a Magento 2 developer, you often find yourself needing to override core files to extend or modify their behavior. This could be for adding custom functionality, changing the layout, or even completely replacing an existing file with your own implementation.

Understanding the Core File Structure

Magento 2 uses a modular architecture where each module has its own directory structure. The core files are usually located in the vendor/magento directory. Some of the key directories include:

  • app/code: Contains all the Magento modules, including both core and third-party extensions.
  • view/frontend/templates: Holds the PHP templates (.phtml files) used by the frontend.
  • templates/page/html/head.phtml: A typical example would be this file, which includes CSS and JavaScript files in the head section of every page.

Overriding PHTML Files

To override a core phtml file, you need to follow these steps:

  1. Create a Magento extension: If you haven’t already, create your own custom extension. This could be as simple as a module with no code outside of the configuration XML files.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Your_Company_Name_YourExtension" setup_version="1.0.0"/>
</config>
  1. Copy the file to your theme: The next step is to copy the file you want to override from vendor/magento into your custom theme directory. For example, if you were overriding head.phtml, it would go inside app/design/frontend/Your_Vendor/your_theme/Magento_Theme/templates/page/html/.

  2. Update the file: Make changes to the file as needed.

<!-- In head.phtml -->
<?php echo $this->getChildHtml('before_body_start') ?>
  1. Flush cache and deploy static content: After making your changes, you need to flush Magento’s cache and deploy static content for the changes to take effect:
bin/magento c:f
bin/magento s:d -s

Overriding Layout Files

Layout files define the structure of a page. To override them:

  1. Create a layout file: Create your layout XML file in app/design/frontend/Your_Vendor/your_theme/Magento_Theme/layout/. For example, to override the layout for the contact index page (contact_index_index), you would create a file called contact_index_index.xml.

  2. Define your overrides: Inside the XML file, define which elements you want to add or modify.

<!-- In contact_index_index.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Page/etc/page.xsd">
    <body>
        <!-- Add a new container -->
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" template="your_module::custom-block.phtml"/>
        </referenceContainer>

        <!-- Modify an existing block -->
        <move element="contactform" destination="content"/>
    </body>
</page>

Remember, the template attribute should point to your custom phtml file located in your module’s view directory.

Conclusion

Overriding core files is a powerful feature that allows you to extend or modify Magento 2’s behavior. However, it requires a good understanding of the system and careful coding to avoid conflicts or unexpected side effects.

Always ensure you have a backup before making changes, and test thoroughly in a development environment before deploying to production. If you’re looking for more advanced techniques like plugin overrides or custom module development, I recommend checking out our guide on Extending Magento 2.

Happy coding!

Magento 2 Extensions Themes Configuration

Our network