MetaTags. Version: 0.6 Author: GNot Author URI: http://www.g-loaded.eu/ */ /* License: GPLv2 Compatibility: All What it does: Adds the post's categories and excerpt to the "keywords" and the "description" XHTML meta tags respectively, while viewing single posts. It also adds the blog's description and categories to the "description" and the "keywords" meta tags respectively on the home page. Installation: Put the add-meta-tags.php file in your /wp-content/plugins/ directory and activate through the administration panel. Copyright (C) George Notaras (http://www.g-loaded.eu/) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Changelog * Wed Jan 10 2007 - v0.6 - A description filter was added. Double quotes and other special characters are are now transformed to HTML entities. Also, line-feed characters and multiple consecutive empty spaces are replaced by a single empty space. Thanks to Ronald for the bug report. * Tue Nov 28 2006 - v0.5 - Added ability to override the keywords that derives from the post's categories with custom keywords from a custom field, named "keywords". (for single posts) - When overriding keywords, %cats% is replaced with the post's categories. - Added ability to override the description that derives from post's excerpt with a custom description from a custom field, named "description". (for single posts) - Added administration panel. No more manual editing. * Sun Nov 05 2006 - v0.4 - The plugin also adds "description" and "keywords" meta tags on the home page. * Wed Oct 04 2006 - v0.3 - Plugin information update * Fri Jan 6 2006 - v0.2 - Duh! I had forgotten to strip the tags from the excerpt. Now it's fixed. * Thu Jan 5 2006 - v0.1 - Initial release */ // Admin Panel function amt_add_pages() { add_options_page('Meta Tags Options', 'Meta Tags', 8, __FILE__, 'amt_options_page'); } function amt_options_page() { if (isset($_POST['info_update'])) { // Site description option if (empty($_POST['amt_site_description'])) { // Delete the option from the DB if it's empty delete_option('amt_site_description'); $msg_desc = "Site description deleted from the WordPress database."; } else { $site_description = $_POST['amt_site_description']; update_option('amt_site_description', $site_description); $msg_desc = "Site description saved."; } // Site keywords option if (empty($_POST['amt_site_keywords'])) { // Delete the option from the DB if it's empty delete_option('amt_site_keywords'); $msg_keyw = "Site keywords deleted from the WordPress database."; } else { $site_keywords = $_POST['amt_site_keywords']; update_option('amt_site_keywords', $site_keywords); $msg_keyw = "Site keywords saved."; } _e('

' . $msg_desc . '
' . $msg_keyw . '

'); } else { $site_description = get_option('amt_site_description'); $site_keywords = get_option('amt_site_keywords'); } // Configuration Page _e('

Meta Tags on the Front Page

The options in this section can be used in order to override the default behaviour of the "Add-Meta-Tags" plugin, when it writes the "description" and "keywords" meta tags for the blog\'s front page. Modifying the following settings is completely optional.

Site Description

The following text will be used for the "description" meta tag. If this is left empty, then the blog\'s description from the General Options (Tagline) will be used.

Site Keywords

Provide a comma-delimited list of keywords for your blog. If this field is left empty, then all of your blog\'s categories will be used as keywords for the "keywords" meta tag.

Example: keyword1, keyword2, keyword3

Info: This plugin stores two options in the WordPress database. By leaving the above fields empty and hitting the "Update Options" button, the options are removed from the WordPress database. Although it is not necessary, you should consider doing this before uninstalling the plugin, so no trace is left behind.

Meta Tags on Single Posts

Although no configuration is needed in order to put meta tags on single posts, the following information will help you customize them.

By default, when a single post is displayed, the post\'s excerpt and the post\'s categories are used in the "description" and the "keywords" meta tags respectively.

It is possible to override them by providing a custom description in a custom field named "description" and a custom comma-delimited list of keywords by providing it in a custom field named "keywords".

Furthermore, when overriding the post\'s keywords, but you need to include the post\'s categories too, you don\'t need to type them, but the tag %cats% can be used.

Example: keyword1, keyword2, %cats%, keyword3, keyword4

'); } function amt_clean_desc($desc) { // This is a filter for the description metatag text $desc = stripslashes($desc); $desc = strip_tags($desc); $desc = htmlspecialchars($desc); $desc = preg_replace('/(\n+)/', ' ', $desc); $desc = preg_replace('/( +)/', ' ', $desc); return $desc; } function amt_get_keywords_from_post_cats() { global $posts; $postcats = ""; foreach((get_the_category($posts[0]->ID)) as $cat) { $postcats .= $cat->category_nicename . ', '; } $postcats = substr($postcats, 0, -2); return $postcats; } function amt_add_meta_tags() { global $posts; // Get the options for the front page from the Database $site_description = get_option('amt_site_description'); $site_keywords = get_option('amt_site_keywords'); if ( is_single() ) { // Custom Field names $desc_fld = "description"; $keyw_fld = "keywords"; // Add META tags to Single Page // Description - Custom post field "description" overrides post's excerpt. $desc_fld_content = get_post_meta($posts[0]->ID, $desc_fld, true); if ( !empty($desc_fld_content) ) { // If there is a custom field, use this $my_metatags = "\n"; } else { // Else, use the post's excerpt. $my_metatags = "\n"; } // Keywords - Custom post field "keywords" overrides post's categories // %cats% is a replaced by the post's categories $keyw_fld_content = get_post_meta($posts[0]->ID, $keyw_fld, true); if ( !empty($keyw_fld_content) ) { // If there is a custom field $keyw_fld_content = strtolower(str_replace("%cats%", amt_get_keywords_from_post_cats(), $keyw_fld_content)); $my_metatags .= "\n\n"; } else { // Else use only the post's categories. $my_metatags .= "\n\n"; } } elseif ( is_home() ) { // Add META tags to Home Page // Description if ( empty($site_description) ) { // If $site_description is empty, then use the blog description from the options $my_metatags = "\n"; } else { // If $site_description has been set, then use it in the description meta-tag $my_metatags = "\n"; } // Keywords if ( empty($site_keywords) ) { // If $site_keywords is empty, then all the blog's categories are added as keywords $my_metatags .= "\ncategory_nicename)) . ', '; } $my_metatags = rtrim($my_metatags, " ,\n") . "\" />\n"; } else { // If $site_keywords has been set, then these keywords are used. $my_metatags .= "\n\n"; } } echo $my_metatags; } add_action('admin_menu', 'amt_add_pages'); add_action('wp_head', 'amt_add_meta_tags'); ?>