Custom meta info sets the <title>, the meta description and the meta keywords of user pages from a list of rules
in the site config, without touching the modules or templates that make the pages. It is off by default; a site turns it on with
$PROJECT_CONF['tpl']['CUSTOM_META_INFO'] = true;. The class is yf_custom_meta_info
(_class('custom_meta_info')): an output filter of the finished page, on the user side only.
Rules
$PROJECT_CONF['custom_meta_info']['RULES'] maps a key to the fields title, description and
keywords:
$PROJECT_CONF['custom_meta_info']['RULES'] = [
'news' => ['title' => 'News', 'description' => 'News of the shop', 'keywords' => 'news, shop'],
'news/show' => ['title' => 'A news item'],
'news/show/5' => ['title' => 'Our new shop is open'],
'/about' => ['title' => 'About us', 'description' => 'Who we are'],
];
- A key that starts with
/is the path of the page URL: the query string, the path ofWEB_PATH(a site in a sub-folder) and a trailing slash are left out;/is the home page. - Any other key is the
object,object/actionorobject/action/idof the request. - Every key that matches applies, from the general to the specific one:
news,news/show,news/show/5, then the URL path. A field of a more specific rule wins; an empty field leaves the one before it. - A field no rule sets stays as the page has it.
Hook
For meta kept elsewhere (a table of the site, a CMS), $PROJECT_CONF['custom_meta_info']['HOOK'] is a callable that gets
the meta the rules made and the keys of the request (general first, the URL path last) and returns the meta:
$PROJECT_CONF['custom_meta_info']['HOOK'] = function (array $meta, array $keys) {
$row = db()->get('SELECT title, description FROM ' . db('my_page_meta') . ' WHERE path = \'' . _es(end($keys)) . '\'');
return $row ? array_merge($meta, array_filter($row)) : $meta;
};
A site class custom_meta_info extends yf_custom_meta_info may override _get_meta(array $keys) instead.
What changes in the page
Only the <head> of the page: the text of <title> is replaced, the content of
<meta name="description"> and <meta name="keywords"> is replaced, and a tag the page does not have is
added before </head>. Values are plain text: line breaks become spaces and the text is HTML-escaped, so a rule cannot
add markup. When there is no rule for the page, no <head>, or the hook fails (an exception, no array), the page is
sent as it is and a warning is logged.