Change any page title in Magento

Featured Image

Here’s a quicky one :) How do you change page title of every Magento page if some titles are hardcoded into controller?

Since some controllers in Magento likes to force page titles like

$this->loadLayout();
...
$this->getLayout()->getBlock('head')->setTitle($this->__('My Account'));
$this->renderLayout();

we can’t go with setTitle through layout, since controller is setting title right before rendering.

There is a simple solution for this, use another variable for title display :)

In page/html/head.phtml template replace default

<title>< ?php echo $this->getTitle() ?></title>

with

<title>
< ?php echo ($this->getForcedTitle()) ? Mage::getStoreConfig('design/head/title_prefix').' '.$this->getForcedTitle().' '.Mage::getStoreConfig('design/head/title_suffix') : $this->getTitle() ?>
</title>

or if you don’t wish to use title prefix/sufix added from Magento admin, simply with

<title>
< ?php echo ($this->getForcedTitle()) ? $this->getForcedTitle() : $this->getTitle() ?>
</title>

and now just set “forced” title for pages through layout files

<reference name="head">
<action method="setForcedTitle"><title>Account Dashboard</title></action>
</reference>

Example for My Account page in layout/customer.xml:
<customer_account>
<reference name="head">
<action method=”setForcedTitle”><title>Account Dashboard</title></action>
</reference>

</customer_account>

;)

30
Top

Care to rate this post?

Author

Ivan Weiler

Technical Educator and Consultant

Ivan is a Technical Educator and Consultant. He gained lots of experience managing some of the most complex Magento projects we had at Inchoo.

Other posts from this author

Discussion 30 Comments

Add Comment
  1. Great Post! Thanks!
    I’d like to add as a sufix, the special price in products pages only, would you have an idea for this?

    Cheers

  2. Dmitry

    What about translation this titles?

  3. Vishal

    Thanks …
    This is very useful …

    Overall this site is good … site having very useful tips & Hints ….

    Keep Rokin……….

  4. Razaul

    Thanks….
    Its working fine

  5. Niels

    It works here, thanks!
    But how about changing the titles of Review pages (dynamically)?
    i.e. “{Productname} Review”
    Seen a couple of solutions but those didn’t work out.

    I hope someone can help me out on this problem.

    Thanks in advance!

  6. Tetef

    Thanks a lot, exactly what I need!

  7. Lex

    Nice Tip. Here is another way to set forced title without change any file.

    To set “forced” title for pages you can do this in the magento backend. Go to CMS/Pages and choose your page, then in the Design tab put the code in the field “Layout Update XML”. Save and refresh the cash.

    Of course, first you need to do the first step in this post (change the file In page/html/head.phtml)

    Regards.

  8. vipin

    thnzx a lot

  9. I tried this on the Contact Form but it did not change the default title of Magento Commerce, any ideas?

  10. Hi all,
    I need to change the title ONLY on product pages.

    any idea?

  11. Hi you all,

    I am a fresher with Magento :), struggling finding a

    way to edit the title tags and description tags in just

    some pages (not products pages) but I just can’t find

    them in the backend.

    Please help?

    Thank you guys

  12. Hi Inchoo!

    Thanks so much for this tutorial. You saved me! :)

  13. Hi,

    This works very well for me. Thanks a lot.

    However I was wondering how does the setForcedTitle function work. I could not find any equivalent function.

    In which case can I use setForcedDescription for changing the meta description of a page.

  14. Justin

    What about forcing title directly from a phtml page? Im trying to get dynamic page titles working.

  15. thanks it help me a lot

  16. I’ve been looking for a solution to this problem for a day and never even thought of using another variable! Thank you!

  17. Dick

    This is great. However, I’m looking for a way to add the Short Description after the Product Name to the page titles on product pages. For this site, the Short Descriptions are very short and needed to differentiate similarly named products.

  18. Hey! Thanks for that! Best wishes! Tobi

  19. Igor

    Just tried this method and it worked. However, it changed titles of all of my pages to the one I’ve typed in XML. Not complete solution :(

  20. DanCapitanDePlai

    Ok,but what if i want to insert in the title a filter that has been selected in the layer navigation ?

  21. joop

    i like your post.
    I want to show the price of my items in the title.
    can you help me out?

  22. Kamil

    To translate this title just add

     translate="title"

    to xml and it should looks like:

    <action method="setForcedTitle" translate="title"><title>Account Dashboard</title></action>
  23. really thanks for you. another method is to use

    titleTest

    in your config xml file

  24. Rachel

    Excellent! Works perfectly in 1.7 CE :D

  25. perfect!!! thanx :)

  26. Dan

    I love you Ivan. Nice one!

  27. Alexa

    Alternatively, you can keep the

    <title><?php echo $this->getTitle() ?></title>
    

    and just add this block at the top of the head.phtml page -

    if ($this->getForcedTitle()) {
        $this->setTitle($this->getForcedTitle());
    }
    
  28. Rick Schippers

    Thank you. You made my day ;-)

  29. super awesome!!!

  30. deepak

    How can i update meta tags in bulk througn php file?

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top