Meta Tags Generator — PHP source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
// meta-tags-generator — build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (og_title/og_description/og_url
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. PHP port (canonical TS:
// src/lib/meta-tags.ts; Go twin:
// cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
// fallbacks, same emission order, same warnings (title > 60,
// description > 160) and the same hard errors (missing title/description,
// non-absolute og:image, non-hex theme-color suppress emission entirely).
// Never throws — errors travel in the result array.
/**
* Build the ordered head-tag set. Takes the input field map (strings, absent
* keys treated as blank); returns ['ok' => bool, 'errors' => string[],
* 'warnings' => string[], 'tags' => string[]]. Hard errors suppress emission
* entirely; length warnings never block emission.
*/
function build_meta_tags(array $in): array
{
$trim = static fn (string $s): string => trim($s);
$errors = [];
$warnings = [];
$title = $trim((string) ($in['title'] ?? ''));
$description = $trim((string) ($in['description'] ?? ''));
$canonical = $trim((string) ($in['canonical_url'] ?? ''));
$ogTitleIn = $trim((string) ($in['og_title'] ?? ''));
$ogTitle = $ogTitleIn !== '' ? $ogTitleIn : $title;
$ogDescriptionIn = $trim((string) ($in['og_description'] ?? ''));
$ogDescription = $ogDescriptionIn !== '' ? $ogDescriptionIn : $description;
$ogImage = $trim((string) ($in['og_image'] ?? ''));
$ogUrlIn = $trim((string) ($in['og_url'] ?? ''));
$ogUrl = $ogUrlIn !== '' ? $ogUrlIn : $canonical;
$themeColor = $trim((string) ($in['theme_color'] ?? ''));
if ($title === '') {
$errors[] = 'Title is required.';
}
if ($description === '') {
$errors[] = 'Description is required.';
}
if ($ogImage !== '' && !preg_match('/^https?:\/\//', $ogImage)) {
$errors[] = 'og:image must be an absolute http(s) URL.';
}
if ($themeColor !== '' && !preg_match('/^#[0-9a-fA-F]{6}$/', $themeColor)) {
$errors[] = 'theme-color must be a #rrggbb hex value.';
}
if ($errors !== []) {
return ['ok' => false, 'errors' => $errors, 'warnings' => [], 'tags' => []];
}
if (strlen($title) > 60) {
$warnings[] = "Title is " . strlen($title) . " characters — search results truncate around 60.";
}
if (strlen($description) > 160) {
$warnings[] = "Description is " . strlen($description) . " characters — search results truncate around 160.";
}
$tags = [
"<title>{$title}</title>",
"<meta name=\"description\" content=\"{$description}\" />",
];
if ($canonical !== '') {
$tags[] = "<link rel=\"canonical\" href=\"{$canonical}\" />";
}
$robots = $trim((string) ($in['robots'] ?? ''));
if ($robots !== '') {
$tags[] = "<meta name=\"robots\" content=\"{$robots}\" />";
}
$hasOg = $ogTitleIn !== '' || $ogDescriptionIn !== '' || $ogImage !== '' || $ogUrl !== '';
if ($hasOg) {
$tags[] = "<meta property=\"og:title\" content=\"{$ogTitle}\" />";
$tags[] = "<meta property=\"og:description\" content=\"{$ogDescription}\" />";
if ($ogImage !== '') {
$tags[] = "<meta property=\"og:image\" content=\"{$ogImage}\" />";
}
if ($ogUrl !== '') {
$tags[] = "<meta property=\"og:url\" content=\"{$ogUrl}\" />";
}
$tags[] = '<meta property="og:type" content="website" />';
}
$twitterCard = (string) ($in['twitter_card'] ?? '');
if ($twitterCard !== '') {
$tags[] = "<meta name=\"twitter:card\" content=\"{$twitterCard}\" />";
}
$twitterSite = $trim((string) ($in['twitter_site'] ?? ''));
if ($twitterSite !== '') {
$tags[] = "<meta name=\"twitter:site\" content=\"{$twitterSite}\" />";
}
if ($themeColor !== '') {
$tags[] = "<meta name=\"theme-color\" content=\"{$themeColor}\" />";
}
return ['ok' => true, 'errors' => [], 'warnings' => $warnings, 'tags' => $tags];
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →