Another problem, another plugin? Managing CMS plugins can become its own cost center if you’re not prudent about selection and inclusion. Here is my most recent case in point. I need to add unique meta descriptions to WordPress. I’ve already added unique titles to this site via yet another plugin. So I found this page on adding one manually, which gets you 99% of the way there. He actually gets you all the way, but a tweak to his instructions brings it together. You can read why he established his conditionals like he did at that link — and you should read it. I would have commented the correction to his site but his comments are closed, so here goes.

This code works well for my purposes. This consolidates his two step-through examples, and I’ve brought the php start tag up to the html, otherwise, there’s a newline right after the opening double quote for the description text.

Setup:

  1. Establish a “description” custom field for your pages. (Perhaps your posts, too, I’m not too worried about those for now, since I’m using WP as a CMS, less so a blog.)
  2. Disable any server-side caching, if in use, which should be SOP, right?
  3. Modify your theme’s header.php file with the following in the meta section:
<meta name="description" content="<?php if (is_single() || is_page()) {
        # Single post / page.
        # Use the 'description' custom field.
        echo get_post_meta($post->ID, 'description', true);
    } elseif (is_category()) {
        # Category page.
        # Use category's description
        echo trim(strip_tags(category_description()));
    } else {
        # Default meta description
        # Blog's description
        echo bloginfo('description');
    } ?> " />

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Performance Optimization WordPress Plugins by W3 EDGE