Use site meta description if no page meta description is given

I’ve been trying to find an answer, but I don’t know the vocab to ask the question:

I’d like the client to be able to write page / meta descriptions for each page. But my hunch is this will get overlooked. Rather than have no page / meta description, is it possible to have a “fallback” value, set on the Site page?

Page blueprint, something like this:

meta_description:
	label: Meta / page description		
	type: text
	fallback: Default text if no text is provided (set on the Site page)

Is something like this possible without using a plugin (which look very complicated)?

Many thanks

You can use a controller to check if the page has meta description, and if not use the one in the site text file.

A php ternary operator is probably the best way to go.

Thanks for helping. From a quick Google search of “php ternary operator” this is well outside my comfort zone.

What does the default do? I was hoping I could set default text, but the text doesn’t display in the Panel or in the HTML.

meta_description:
  label: Meta / page description		
  type: text
  default: Default text used if not over-written

A ternary would just look something like this

$meta = $page->meta_description()->isNotEmpty() ? $page->meta_description() : $site->meta_description();

Then you just return $meta from the controller to the template for display in the HTML head.

Using a default value only works on page creation, not if the page already exist. Why do you want to provide a default if you define it in site anyway? then you can no longer check if the field is empty and fall back to the site default.

Why do you want to provide a default if you define it in site anyway? then you can no longer check if the field is empty and fall back to the site default.

Because that looks well beyond me! I was hoping I could simply hard code a default description (that might get over written by the client).

As I said, the default property only works at page creation. Otherwise you have to add the default value manually to every existing page.

But I don’t really understand what exactly the problem is for you. In the topic title you asked to overwrite the value if info from site. There is an easier way to use the default than the condition:

<?= $page->meta_description()->or($site->meta_description()); ?>

Or you can even set some manually:

<?= $page->meta_description()->or('Some fallback text if field is empty'); ?>

I was only thinking of using the default property because a php ternary operator looks very complicated.

Thanks for the code, that’s brilliant and really easy to use.

If you know what it does, it’s pretty easy to understand. The ternary expression above translates to the following if-else statement, which is much more verbose:

if($page->meta_description()->isNotEmpty()) {
  $meta = $page->meta_description(); 
} else {
  $meta = $site->meta_description();
}

This works
<meta name="description" content="<?= $page->meta_description()->or($site->meta_description()); ?>">

Can I use this, or do I have to use a Controller for some reason?

No, not at all

Brilliant, thank you