PHP + HTML
SEO + Open Graph Block for PHP Layouts
Define global defaults for title, description, canonical and OG image, then let each page override as needed via a <code>$seo</code> array. Keeps your head tags consistent and easy to update.
Usage notes
Drop this into your main layout (for example partials/head.php) and pass in a $seo array from each page.
You can store SEO rows wherever you like and hydrate the array before rendering.
Copy this snippet into your project
Use the full version for learning, or copy it without comments when you just want the bare code.
<?php
// $defaults and $seo should be defined by the page
$title = htmlspecialchars($seo['title'] ?? $defaults['title'] ?? 'Site title', ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($seo['description'] ?? $defaults['description'] ?? '', ENT_QUOTES, 'UTF-8');
$canonical = htmlspecialchars($seo['canonical'] ?? $defaults['canonical'] ?? '', ENT_QUOTES, 'UTF-8');
$ogImage = htmlspecialchars($seo['og_image'] ?? $defaults['og_image'] ?? '', ENT_QUOTES, 'UTF-8');
?>
<title><?= $title ?></title>
<meta name="description" content="<?= $description ?>">
<?php if ($canonical !== ''): ?>
<link rel="canonical" href="<?= $canonical ?>">
<?php endif; ?>
<meta property="og:title" content="<?= $title ?>">
<meta property="og:description" content="<?= $description ?>">
<meta property="og:type" content="website">
<?php if ($canonical !== ''): ?>
<meta property="og:url" content="<?= $canonical ?>">
<?php endif; ?>
<?php if ($ogImage !== ''): ?>
<meta property="og:image" content="<?= $ogImage ?>">
<?php endif; ?>