SEO Settings
You can specify SEO settings on any page of your resource - functional elements of the page code that contain information for browsers and search engines and help optimize the search for the necessary information on the site.
Forming SEO Pages
The formation of SEO pages and their parameters is carried out using the seoTemplates()
method - it sets the settings for the general SEO page templates.
This method should be located in the base
class of the add-on module and return an array of SEO page template settings.
Let's consider an example of implementing the method for an article page.
/**
* Description of seo page templates
* @return array
*/
public function seoTemplates(): array
{
return [
'pages' => [
'article-view' => [
't' => _t('site', 'Article Page'), # template name
'i' => true, # implemented
'inherit' => true, # inherit
'macros' => [ # macros
'textshort' => ['t' => _t('site', 'Short description (up to 150 characters)')],
],
],
...
]
];
}
The page templates are located in an array with the key pages
, and each template has its own unique key, for example, article-view
,
which will be used later to form the SEO tab in the admin panel.
We can distinguish
- a general template for setting SEO parameters for all article pages, which is located in the admin panel in the SEO menu item,
- a specific template for each individual article, located as a tab in the admin panel in the form for editing your page.
Form Tab
Let's see how to create a tab for setting SEO parameters for each individual article and what fields are required in the database for this.
In the form template for editing an article, you need to use the tabSEO(tab_key)
method of the form constructor to set up such a tab, using the previously specified SEO page key in the seoTemplates()
method.
$form->tabSEO('article-view');
For this tab, fields in the database are also required, in the table of the page to which you are setting SEO parameters
CREATE TABLE `bff_articles` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, -- article ID
`title` varchar(255) NOT NULL DEFAULT '', -- title
`content` longtext NOT NULL DEFAULT '', -- content
`enabled` tinyint(1) unsigned NOT NULL DEFAULT '1', -- published
...
`mtemplate` tinyint(1) UNSIGNED NOT NULL DEFAULT '1',
`mtitle` varchar(200) NOT NULL DEFAULT '',
`mkeywords` mediumtext ,
`mdescription` mediumtext ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- mtemplate - indicates whether the general template is applied to SEO parameters;
- mtitle - the page title in the
<title>Article Title</title>
tag; - mkeywords - keywords in the
<meta>
tag with the "keywords" attribute value<meta name="keywords" content="keywords ...">
; - mdescription - keywords in the
<meta>
tag with the "description" attribute value<meta name="description" content="meta description ...">
.
These fields must be included in the form data when it is displayed and validated when it is saved. They can also be multilingual and located in the current table or in a separate language table for translation purposes.
General SEO Template
A general SEO template for all articles is set by adding a submenu item to the SEO module in the admin panel.
To do this, in the menu formation file of your add-on module in the admin panel menu.php
, add the necessary menu item.
<?php
use bff;
bff::adminMenu()
->group('Articles')
->add(_t('articles', 'List'), 'articles', 'listing')
# SEO
->group('SEO')
->add(_t('articles', 'Articles'), 'articles', 'seo_templates_edit')
->access(['articles', 'seo'])
;
Applying SEO settings to a page
Before rendering an article, you need to set its SEO parameters and define macros.
We can set the meta tags for the article page using the setMeta()
method available in the frontend controller of your addon module.
Let's consider an example of how to apply it.
/**
* Article page
* @return string HTML
*/
public function articleView()
{
$articleID = $this->input->getpost('id', TYPE_UINT);
if (empty($articleID)) {
$this->errors->error404();
}
$data = $this->getArticleData($articleID); // article data, should include all SEO parameters
# SEO: Article page
$this->setMeta(
'article-view', // page key defined in the seo template
['textshort' => \tpl::truncate(strip_tags($data['descr']), 150, '...')], // data for macros
$data // page meta data, passed by reference
);
return $this->viewPHP($data, 'article');
}
Note that the macros data is defined in a separate array, and the page meta data is passed by reference.
$data
should contain mtemplate
, mtitle
, mkeywords
, mdescription
considering the current language.