ProcessWire: Best Practice Meta Tags

There are dozens of ways of adding meta tags like title, keywords and description to your website and probaby even more using a CMS. As you may know my favorite CMS is Processwire. So here is the way I'm using it with more or less dynamic SEO meta tags:

Create fields

There are some fields needed for this. The required fields are:

  • meta_title (Text)
  • meta_keywords (Text)
  • meta_description (Text)

Optional fields for a better order are:

  • _meta (FieldsetTabOpen)
  • _meta_END (FieldsetClose, will be created automatically after creating _meta)

Of course you can call them however you want but I'm continuing using this names in this article.

Add them to the template (e.g. basic-page)

As with any field in ProcessWire you have to add them to your template afterwards. I recommend to add them to your "home" template to. Why? I'm going to explain this later.

Added to the end of the template's fields it should look like this:

Insert the Meta Data

If you're now editing a page you should recognize a new tab at the top called "Meta" (or if you didn't add a title "_meta"):

Here you can insert specific meta tags for every page. But what happens if you leave it empty? That's why I recommended to add the meta fields to your "home" template to. Because we're going to use the "home" meta tags as default.

That's everything we have to setup in the backend. Now we need to add some code to the frontend (your template files) to display them:

Display the meta tags

Here is the code you have to add. I think it's self explaining (at least if you're knowing PHP shorthand conditions):

<title><?php echo (($page->meta_title) ? $page->meta_title : $pages->get('/')->meta_title); ?></title>

<meta name="keywords" content="<?php echo (($page->meta_keywords) ? $page->meta_keywords : $pages->get('/')->meta_keywords); ?>" />
<meta name="description" content="<?php echo (($page->meta_description) ? $page->meta_description : $pages->get('/')->meta_description); ?>" /> 

Sorry for the bad redability. Should be more clear after copying it into a text editor.