Step 1:

Create a custom extension based on Magento 2 structure in the folder app/code.

Ex: Magento 2 root directory/app/code/vk/hd/

Step 2:

Create module.xml file in the below path to define your Magento 2 extension.

Magento 2 root directory/app/code/vk/hd/etc/module.xml

<?xml version=“1.0”?>

<config xmlns:xsi=”” xsi:noNamespaceSchemaLocation=”../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd”>

<module name=”vk_hd” setup_version=”2.0.1″></module>


 Step 3:

Create routes.xml in the below path for your custom link. Magento 2 root directory/app/vk/hd/etc/frontend/routes.xml

<?xml version=”1.0″?>

<config xmlns:xsi=”” xsi:noNamespaceSchemaLocation=”../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd”>

<router id=”standard”>

<route id=”hd” frontName=”hd”>

<module name=”vk_hd” />




Step 4:

Create layout file in the below path for a custom header. Magento2 root directory/app/vk/hd/view/frontend/layout/header.xml

<?xml version=”1.0″?>

<page xmlns:xsi=”” layout=”1column” xsi:noNamespaceSchemaLocation=”../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd”>


<referenceBlock name=”header.links”>

<move element=”test-last” destination=”header.links”/>

<block class=”vk\hd\Block\Header” name=”test-link” after=”my-account-link”/>




Step 5:

Create block file that is referred in the layout file in the below path. Magento2 root directory/app/vk/hd/Block/Header.php


namespace vk\hd\Block;

class Header extends \Magento\Framework\View\Element\Html\Link


protected $_template = ‘vk_hd::link.phtml’;

public function getHref()


return__( ‘testing’);


public function getLabel()


return __(‘Test Linking’);




Step 6:

Create template file that is referred to the block file in the below path. Magento2 root directory/app/vk/hd/frontend/templates/link.phtml


<a <?php echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel())?>

<?php echo($block->getCounter()) ? ‘<span>’ . $block->escapeHtml($block->getCounter()) . ‘</span>’ : ”; ?>



Step 7:

After completing the above steps run the below Magento2 upgrade command in the command prompt:

php bin/magento setup:upgrade

Categories: BlogPHP

0 thoughts on “How to Add Custom Header Links in Magento 2”

Leave a Reply

Related Article


World Badminton Championships: Sindhu eases into pre-quarterfinals

NEW DELHI: Top Indian shuttler PV Sindhu stormed into pre-quarterfinals of the women\’s singles accident at the apple Badminton Championships, with a advantageous beeline amateur achievement over Kim Hyo Min of Korea in Glasgow on Read more…


Premier League: Philippe Coutinho has not damaged relationship with Liverpool, says Jurgen Klopp

London: Liverpool administrator Jurgen Klopp says Philippe Coutinho would be accustomed back into the band because the active Brazilan attempts to force a move to Barcelona have not damaged their accord. Coutinho handed in a Read more…


Samsung Bixby Voice now available in 200 countries globally

After accession in the US a ages ago, Samsung today appear that it’s articulation abettor has now gone all-around. Samsung appear through it’s official blog that it’s Bixby articulation abettor available on the Galaxy S8 Read more…