For coders TYPO3 Tech Corner

TYPO3: Set title tag and other meta tags

TYPO3: Set title tag and other meta tags

Since version 9 of TYPO3 there is finally a long-awaited PageTitle and a MetaTag API, with which the title tag and various metatags of a page can be set cleanly depending on the context.

One of the most important points in SEO is the correct setting of the title tag. The description of meaningful meta tags also fill entire websites of various SEO agencies. What interests us more, however, are the technical aspects in the TYPO3 CMS.

Recently, it is no longer necessary to tinker with TypoScript with UserFuncs or the like if you want to set meaningful meta tags in an Extbase ControllerAction on a detail page. Alternatively, this is no longer a problem via the new API with the help of ViewHelpers.

Setting the title tag in TYPO3

We show you a few examples (here in an action):

<?php declare(strict_types = 1); namespace In2code\In2mvadviser\Controller; use In2code\In2mvadviser\PageTitle\Provider; use TYPO3\CMS\Core\Utility\GeneralUtility; class AuthorController extends ActionController { public function detailAction(Author $author): void { // Set a new title tag $titleProvider = GeneralUtility::makeInstance(Provider::class); $titleProvider->setTitle($author->getTitle()); } }

We are expanding the whole thing with our own provider:

<?php declare(strict_types = 1); namespace In2code\In2mvadviser\PageTitle; use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider; class Provider extends AbstractPageTitleProvider { public function setTitle(string $title): void { $this->title = $title; } }

Registration via TypoScript is important here. This is necessary so that TYPO3 can decide which title should be delivered when:

config.pageTitleProviders { adviser { provider = In2code\In2mvadviser\PageTitle\Provider before = altPageTitle,record,seo,news } }
Setting metatags in TYPO3

Setting meta tags such as og: image, description, og:description or twitter:description is even easier and only via PHP:

<?php declare(strict_types = 1); namespace In2code\In2mvadviser\Controller; class AuthorController extends ActionController { public function detailAction(Author $author): void { // Set metatag "og:image" $registry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class); $metaTagManager = $registry->getManagerForProperty('og:image'); $metaTagManager->addProperty('og:image', '/path/to/image.jpg', ['width' => 400, 'height' => 400]); // Set metatags "description", "og:description" and "twitter:description" $descriptionFields = ['description', 'og:description', 'twitter:description']; foreach ($descriptionFields as $field) { $metaTagManager = $registry->getManagerForProperty($field); $metaTagManager->addProperty($field, $author->getDescription()); } } }

Here you can find the official documentation for PageTitle API and MetaTag API.

Add individual Code into Head

Of course you can also bring other, individual code into the head area of your frontend via PHP - here is an example for STYLE:

$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); $pageRenderer->addHeaderData('<style>body{--color-brand:red;}</style>');

By the way: You can see how you can set the title and meta tags directly in FLUID in another blog post.

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news