Add custom link in header in Magento 2


In this blog,  I want to share with you the way how to create a header link in your Magento 2 site.

Before we begin, if you don’t know how to create a simple custom module in Magento 2, please follow the Step 1 in our guide here :

Now let’s code!

Step 1: We will create a CMS page. For example, we create a “Custom Link” page  with an URL such as

Step 2:  Create a default.xml file in the following path:  app\code\Vendor\Module\view\frontend\layout with the following content:

For more details, the previous xml is used to register a Block Class in “Vendor_name\Module_name\Block\Link” . Then, set Label and URL path for it in the arguments tag.

Step 3:  Create the Block class Link with the following content:

Well done, we just completed. If your code works fine, it should be like the one that I created inside this image: a Membership Plans link beside the sign in button on top of the page.



Comments (3)

  1. suthanalley
    Thank-you for sharing this amazing tutorial. I just want to ask that i am doing the same thing following this tutorial but i am having some errors in the code while implementing and i am unable to detect those errors. class Link extends MagentoFrameworkViewElementHtmlLink { /** * Render block HTML. * * @return string */ protected function _toHtml() { if (false = $this->getTemplate()) { return parent::_toHtml(); } return ‘ getLinkAttributes() . ‘ >’ . $this->escapeHtml($this->getLabel()) . ‘ ‘; } }

Speak Your Mind

87 − = 85