<?php
/*
Plugin Name: Head META Description
Plugin URI: http://guff.szub.net/head-meta-description/
Description: Insert HTML META description tag: excerpt/content brief for post/Page, description for category, and blog tagline for everything else.
Author: Kaf Oseo
Version: R1.1.2
Author URI: http://szub.net

    Copyright (c) 2005, 2006 Kaf Oseo (http://szub.net)
    Head META Description is released under the GNU General Public
    License (GPL) http://www.gnu.org/licenses/gpl.txt

    This is a WordPress plugin (http://wordpress.org).

~Changelog:
R1.1.2 (Jan-03-2006)
Grrr. More of same. {crosses fingers}

R1.1.1 (Jan-02-2006)
Further repairs on category description handling.

R1.1 (Dec-28-2005)
WordPress 2.0 update: Modified category description handling.
*/

function head_meta_desc() {
    
/* >> user-configurable variables */
    
$default_blog_desc ''// default description (setting overrides blog tagline)
    
$post_desc_length  20// description length in # words for post/Page
    
$post_use_excerpt  1// 0 (zero) to force content as description for post/Page
    
$custom_desc_key   'description'// custom field key; if used, overrides excerpt/content
    /* << user-configurable variables */

    
global $cat$cache_categories$wp_query$wp_version;
    if(
is_single() || is_page()) {
        
$post $wp_query->post;
        
$post_custom get_post_custom($post->ID);
        
$custom_desc_value $post_custom["$custom_desc_key"][0];

        if(
$custom_desc_value) {
            
$text $custom_desc_value;
        } elseif(
$post_use_excerpt && !empty($post->post_excerpt)) {
            
$text $post->post_excerpt;
        } else {
            
$text $post->post_content;
        }
        
$text str_replace(array("\r\n""\r""\n""  "), " "$text);
        
$text str_replace(array("\""), ""$text);
        
$text trim(strip_tags($text));
        
$text explode(' '$text);
        if(
count($text) > $post_desc_length) {
            
$l $post_desc_length;
            
$ellipsis '...';
        } else {
            
$l count($text);
            
$ellipsis '';
        }
        
$description '';
        for (
$i=0$i<$l$i++)
            
$description .= $text[$i] . ' ';

        
$description .= $ellipsis;
    } elseif(
is_category()) {
        
$category $wp_query->get_queried_object();
        
$description trim(strip_tags($category->category_description));
    } else {
        
$description = (empty($default_blog_desc)) ? trim(strip_tags(get_bloginfo('description'))) : $default_blog_desc;
    }

    if(
$description) {
        echo 
"<meta name=\"description\" content=\"$description\" />\n";
    }
}

add_action('wp_head''head_meta_desc');
?>