clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Make WordPress SEO friendly with a proper title and description tags

Filed under: WordPress/PHP— Tagged with: seo, php

Someone said something like "if you're ever been charged of SEO, you've bee cheated". Having the basics right, gets you long way.

This post shows you how to use post excerpt and custom fields as description meta tag. And how to use the title right.

There are a lot of SEO plugins out there, you can use one of them, they do pretty much the same job.

Title

For this post the title looks something like make-wordpress-seo-friendly-with-a-proper-title-and-description-tags — clubmate.fi. It's important that it shows the title of the page first rather than the site name. The title of the page is important, thats what people are searching for. See below how to do that.

if (function_exists('is_tag') && is_tag()) {
  single_tag_title("Tag Archive for ""); echo '" - '; }
elseif (is_archive()) {
  wp_title(''); echo ' Archive - '; }
elseif (is_search()) {
  echo 'Search for "' . wp_specialchars($s) . '" - '; }
elseif (!(is_404()) && (is_single()) || (is_page()) && !(is_front_page())) {
  wp_title(''); echo ' — '; }
elseif (is_404()) {
  echo 'Not Found - '; }
if ((is_home()) || (is_front_page())) {
  bloginfo('name'); echo ' — '; bloginfo('description'); }
else {
  bloginfo('name'); }
if ($paged>1) {
  echo ' — page '. $paged;
}

Source

It can be included easily to keep header.php clean.

<title><?php include "adds/title.php"; ?></title>

Description meta tag

It's this one:

<meta name="description" content="Description here" />

This is what Google shows under the title of the site in the search results. So, it makes sense to slap the beginning of the site content there (Google starts cutting it after 150-160 characters).

Here's how to do that.

global $post;
$cont = strip_tags($post->post_content); // Strip markup
$cont = str_replace(array("\n", "\r", "\t"), ' ', $cont); // Change line breaks and tabs to single space
$cont = str_replace(array('"',"'"), '', $cont); // Strip " and '
$cont = substr($cont, 0, 160); // Set the length to 160

Put that into a separate file and call it description.php, and include it in the header, like this.

<meta name="description" content="<?php include "adds/description.php"; ?>">

What if you have vital data in custom fields? No worries, they're easy to add. I'm using WPAlchemy metabox helper class here, but you can use normal custom fields as well.

global $custom_metabox; // Needed for the metabox class
$custom_metabox->the_meta(); // Needed for the metabox class
$what   = $custom_metabox->get_the_value('what'); //Get the values
$what_i = $custom_metabox->get_the_value('client');
$status = $custom_metabox->get_the_value('status');
$link   = $custom_metabox->get_the_value('link');

// Same as in the above example
global $post;
$cont = strip_tags($post->post_content);
$cont = str_replace(array("\n", "\r", "\t"), ' ', $cont);
$cont = str_replace(array('"',"'"), '', $cont);
$cont = substr($cont, 0, 160);

// Echo them out to your liking, in this case separated with spaces
echo $what . ' ' . $status . ' ' . $client . ' ' . $link . ' ' . $cont;

One more thing. The description most likely should be different on archive page and home page than on a single post page. Then you can just conditionally add different bits to your liking.

// Front page
if (is_front_page()) :
  global $post;
  $cont = strip_tags($post->post_content);
  $cont = str_replace(array("\n", "\r", "\t"), ' ', $cont);
  $cont = str_replace(array('"',"'"), '', $cont);
  $cont = substr($cont, 0, 160);

  echo get_bloginfo('title') . ' ' . get_bloginfo('description') . $cont;

// Archive, no content here
elseif (is_archive()) :
  echo get_bloginfo('title') . ' ' . get_bloginfo('description');

// Page or single post, using the custom fields and post content
elseif (is_page() || is_single()) :
  // Custom fields
  global $custom_metabox;
  $custom_metabox->the_meta(); //Call this once
  $what   = $custom_metabox->get_the_value('what');
  $status = $custom_metabox->get_the_value('status');
  $client = $custom_metabox->get_the_value('for_long');
  $link   = $custom_metabox->get_the_value('link');

  // Content of the post
  global $post;
  $cont = strip_tags($post->post_content);
  $cont = str_replace(array("\n", "\r", "\t"), ' ', $cont);
  $cont = str_replace(array('"',"'"), '', $cont);
  $cont = substr($cont, 0, 160);

  // Echo out
  echo $what . ' ' . $status . ' ' . $client . ' ' . $link . ' ' . $cont;
endif;

In SEO the actual content of the post is the most important thing, of course. With out it, none of this makes any difference.

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!