For coders TYPO3 Tech Corner

TYPO3: Set title tag and other meta tags

TYPO3: Set title tag and other meta tags

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.

Back

"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