Instantly share code, notes, and snippets.
<?php | |
/* | |
Plugin Name: Custom META Tags | |
Description: Add custom META tags on a per-page/post basis using custom fields. | |
Author: Keith Solomon | |
Version: 1.0 | |
Author URI: http://solowebdesigns.net/ | |
*/ | |
/* | |
Custom head META tags | |
This plugin will build cutom META (descripton and keyword) tags for your site. You can set defaults for both using the settings below. | |
It will also use the custom fields defined below to fill in this data. If you choose not to use custom fields, it will build per-post/page | |
tags using post tags for keywords and either the post/page content or excerpt for the description. | |
*/ | |
function head_meta() { | |
// Change the settings below to suit your needs. | |
$default_blog_desc = ''; // Default blog description | |
$default_blog_kw = ''; // Default blog keywords | |
$post_desc_length = 20; // description length in # words for post/Page | |
$post_use_excerpt = 0; // 0 (zero) to force content as description for post/Page | |
$custom_desc_key = 'description'; // custom field key; if used, overrides excerpt/content | |
$custom_kw_key = 'keywords'; // custom field key; if used, overrides post tags | |
// Stop editing here. | |
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]; | |
$custom_kw_value = $post_custom["$custom_kw_key"][0]; | |
if ($custom_kw_value) { | |
$keywords = $custom_kw_value; | |
} else { | |
$posttags = get_the_tags(); | |
if ($posttags) { | |
foreach ($posttags as $tag) { | |
$keywords .= $tag->name.','; | |
} | |
} | |
} | |
if ($custom_desc_value) { | |
$text = $custom_desc_value; | |
} elseif ($post_use_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; | |
$keywords = $default_blog_kw; | |
} | |
echo "<!-- Meta tags built by Custom META Tags -->\n"; | |
if ($keywords) { | |
echo "<meta name=\"keywords\" content=\"$keywords\" />\n"; | |
} | |
if ($description) { | |
echo "<meta name=\"description\" content=\"$description\" />\n"; | |
} | |
} | |
add_action('wp_head', 'head_meta'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment