cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Coding in head.phtml (meta,title,description)

Custom Coding in head.phtml (meta,title,description)

Hi Experts,

Good Day!

 

I need to add some custom code inside the head tag to manipulate the tile and meta tags. in magento i it was easy to add in head.phtml but i am unable to find the same file in magento 2. please help me to accomplish this task.

 

Regards,

zia

Tags (1)
9 REPLIES

Re: Custom Coding in head.phtml (meta,title,description)

Hi @muhammad_zia ur rahman

 

You can directly add title and keywords in head tag from the backend of the magento admin panel !

 

Here is the link of detail steps from where you can go and add those title and keywords for the same - https://docs.magento.com/m2/ce/user_guide/design/html-head.html

 

this is for global let me know if you wanted to add still in head.phtml !

 

Hope it helps 

 

if issue solved,Click Kudos & Accept as Solution

Re: Custom Coding in head.phtml (meta,title,description)

Hi @ManthanDave,

 

thanks for your reply.

 

Note: actually i want to do ON PAGE SEO.

 

I want to add some php scripts to handle the title and meta description dynamically. how can i do this?

 

Regards,

zia 

Re: Custom Coding in head.phtml (meta,title,description)

Hi @muhammad_zia ur rahman

 

ok i understand your problem here is the link for the same - https://stackoverflow.com/questions/48167553/magento-2-add-custom-script-just-after-head-tag

 

This will helps you to achieve add custom script just after head tag !

 

Same way if you wanted to do it via block(php) and phtml - you can do it by this way refer this link - https://magento.stackexchange.com/questions/147602/add-block-in-head-tag-in-magento-2/147606

 

Location of the file is : vendor/magento/moduletheme/view/frontend/templates/html/header.phtml

if issue solved,Click Kudos & Accept as Solution

Re: Custom Coding in head.phtml (meta,title,description)

By default head.phtml file is not exist in magento 2,

There is header.phtml file is available for header related information.

header.phtml file location is,

 

vendor/magento/module-theme/view/frontend/templates/html/header.phtml

You can directly add your code from admin panel.

 

Go  To Content -> Design -> Configuration,

Edit your theme,

Add code inside, Html Head Section,

Add your code in Script and Stylesheet field for Script and Style related code.

 

If Issue Solved, Click Kudos/Accept As solutions.

Re: Custom Coding in head.phtml (meta,title,description)

I already checked these links. here did not mention clearly which file has to create and where we can add php code. something like that. 

Re: Custom Coding in head.phtml (meta,title,description)

Hi @Rakesh Jesadiya,

thanks for your reply. 

Actually i need to handle on page seo. so i need to handle title and meta description dynamically with php code. i need to add this code into .phtml file. how can i do this! that is the issue right now.

Re: Custom Coding in head.phtml (meta,title,description)

vendor/magento/module-theme/view/frontend/templates/html/header.phtml

you need to override above file to your theme level and you can do it. 

If Issue Solved, Click Kudos/Accept As solutions.

Re: Custom Coding in head.phtml (meta,title,description)

@Rakesh Jesadiya,

 

i found this file. but i did not see any coding regarding title, and description. or meta title.

Re: Custom Coding in head.phtml (meta,title,description)

Don't  change in core files. Just follow below steps:

  1. Create a new phtml file in app\design\frontend\<Vendor Name>\<theme name>\Magento_Theme\templates\html
  2. Add your custom code in this file.
  3. After that call this file in the default_head_blocks.xml. File is available here app\design\frontend\<Vendor Name>\<theme name>\Magento_Theme\layout

In head blocks write

<block class="Magento\Framework\View\Element\Template" name="head_file"  template="Magento_Theme::html/<file name>.phtml">

 

Also you can add like below in default.xml
<referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="frontuser" template="Magento_Theme::html/<filename>.phtml"/>
</referenceBlock>