<?php
/**
* Automatically insert meta description tag into posts/pages.
* - can be configured to use either first X chars/words of the post content or post excerpt if available
* - can use category description for category archive pages if available
* - can use tag description for tag archive pages if available
* - can use blog description for everything else
* - can use a default description if no suitable value is found
* - can use the value of a custom field as description
*
* @usage
* // add a custom configuration via filter
* function set_meta_desc_settings( $settings ) {
* return array( 'length' => 10, 'length_unit' => 'char|word', 'use_excerpt' => true, 'add_category_desc' => true, 'add_tag_desc' => true, 'add_other_desc' => true, 'default_description' => '', 'custom_field_key' => '' );
* }
* add_filter( 'meta_desc_settings', 'set_meta_desc_settings' );
* add_action( 'wp_head', 'meta_desc' );
*
* @author Thorsten Ott
*/
function meta_desc() {
$default_settings = array(
'length' => 25, // amount of length units to use for the meta description
'length_unit' => 'word', // the length unit can be either "word" or "char"
'use_excerpt' => true, // if the post/page has an excerpt it will overwrite the generated description if this is set to true
'add_category_desc' => true, // add the category description to category views if this value is true
'add_tag_desc' => true, // add the category description to category views if this value is true
'add_other_desc' => true, // add the blog description/tagline to all other pages if this value is true
'default_description' => '', // in case no description is defined use this as a default description
'custom_field_key' => '', // if a custom field key is set we try to use the value of this field as description
);
$settings = apply_filters( 'meta_desc_settings', $default_settings );
extract( shortcode_atts( $default_settings, $settings ) );
global $wp_query;
if( is_single() || is_page() ) {
$post = $wp_query->post;
// check for a custom field holding a description
if ( !empty( $custom_field_key ) ) {
$post_custom = get_post_custom_values( $custom_field_key, $post->ID );
if ( !empty( $post_custom ) )
$text = $post_custom[0];
}
// check for an excerpt we can use
else if( $use_excerpt && !empty( $post->post_excerpt ) ) {
$text = $post->post_excerpt;
}
// otherwise use the content
else {
$text = $post->post_content;
}
$text = str_replace( array( "\r\n", "\r", "\n", " " ), " ", $text ); // get rid of all line breaks
$text = strip_shortcodes( $text ); // make sure to get rid of shortcodes
$text = apply_filters( 'the_content', $text ); // make sure it's save
$text = trim( strip_tags( $text ) ); // get rid of tags and html fragments
if ( empty( $text ) && !empty( $default_description ) )
$text = $default_description;
} else if( is_category() && true == $add_category_desc ) {
$category = $wp_query->get_queried_object();
$text = trim( strip_tags( $category->category_description ) );
if ( empty( $text ) && !empty( $default_description ) )
$text = $default_description;
} else if( is_tag() && true == $add_tag_desc ) {
$tag = $wp_query->get_queried_object();
$text = trim( strip_tags( $tag->description ) );
if ( empty( $text ) && !empty( $default_description ) )
$text = $default_description;
} else if ( true == $add_other_desc ) {
$text = trim( strip_tags( get_bloginfo('description') ) );
if ( empty( $text ) && !empty( $default_description ) )
$text = $default_description;
}
if ( empty( $text ) )
return;
if ( 'word' == $length_unit ) {
$words = explode(' ', $text, $length + 1);
if ( count( $words ) > $length ) {
array_pop( $words );
array_push( $words, '...' );
$text = implode( ' ', $words );
}
} else {
if ( strlen( $text ) > $length ) {
$text = mb_strimwidth( $text, 0, $length, '...' );
}
}
if ( !empty( $text ) ) {
echo "\n<meta name=\"description\" content=\"$text\" />\n";
}
}
?>
sourcebench 1:53 pm on February 1, 2010 Permalink |
Simple parallel processing with Bash using “wait” and “jobs” – http://bit.ly/92T7jY
This comment was originally posted on Twitter