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>

;)

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


About 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.

Read more posts by Ivan / Visit Ivan's profile

30 comments

  1. 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());
    }
  2. 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>
  3. Ok,but what if i want to insert in the title a filter that has been selected in the layer navigation ?

  4. 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 :(

  5. 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.

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

  7. 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.

  8. 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

  9. 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.

  10. 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!

  11. Thanks …
    This is very useful …

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

    Keep Rokin……….

  12. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.