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.

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

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