A practical meta-tag snippet for Kirby

Meta tags will not rescue weak content, but they do give search engines and social platforms a cleaner description of each page. The useful fields here are the canonical URL, page title, description, and Open Graph data.

This is the example I used for Kirby sites in 2020. The home page takes its title and description from the site object; other pages use their own title and a 140-character excerpt.

<link rel="canonical" href="<?php echo html($page->url()) ?>" />
    <?php if($page->isHomepage()): ?>
      <title><?php echo html($site->title()) ?></title>
    <?php else: ?>
      <title><?php echo $page->title()->html() ?> | <?php echo $site->title()->html() ?></title>
    <?php endif ?>

    <?php if($page->isHomepage()): ?>
    <meta name="description" content="<?php echo $site->description()->html() ?>">
    <?php else: ?>
    <meta name="description" content="<?php echo $page->text()->excerpt(140) ?>">
    <?php endif ?>

    <meta name="publisher" content="<?php echo html($site->author()) ?>" />
    <meta name="copyright" content="<?php echo html($site->author()) ?>" />
    <meta name="robots" content="index,follow" />
    <?php if($page->isHomepage()): ?>
    <meta property="og:title" content="<?php echo html($site->title()) ?>" />
    <?php else: ?>
    <meta property="og:title" content="<?php echo html($page->title()) ?> | <?php echo html($site->title()) ?>" />
    <?php endif ?>
    <?php if($page->isHomepage()): ?>
    <meta property="og:type" content="website" />
    <?php else: ?>
    <meta property="og:type" content="article" />
    <?php endif ?>
    <meta property="og:url" content="<?php echo html($site->url()) ?>" />
    <meta property="og:image" content="<?php echo url('assets/images/image.png') ?>" />

    <?php if($page->isHomepage()): ?>
    <meta property="og:description" content="<?php echo html($site->description()) ?>" /><?php else: ?>
    <meta property="og:description" content="<?php echo html($page->text()->excerpt(140)) ?>
    <?php endif ?>

    <?php if($page->isHomepage()): ?>
    <meta itemprop="name" content="<?php echo html($site->title()) ?>">
    <?php else: ?>
    <meta itemprop="name" content="<?php echo html($page->title()) ?> | <?php echo html($site->title()) ?>">
    <?php endif ?>
    <?php if($page->isHomepage()): ?>
    <meta itemprop="description" content="<?php echo html($site->description()) ?>">
    <?php else: ?>
    <meta itemprop="description" content="<?php echo html($page->text()->excerpt(140)) ?>">
    <?php endif ?>

Replace the Open Graph image with a real default image for the site. Also check the rendered source on both the home page and an article page. Conditional template code is exactly where an unclosed attribute or missing field tends to hide.

For a larger site, I would move the repeated home-page checks into a small helper or prepare the values before printing the tags. The output stays the same, but the template becomes easier to audit.