HTML Meta Tags – A tutorial in writing a WordPress Plug-in



The Solution

Fortunately for us, WordPress allows us to create plug-ins to enhance the general functionality of our blogs. We will need a means to save the unique data to a database and retrieve it when necessary. We have already determined that we will use the built-in gizmo for placing our meta data in the header. The WordPress web site offers assistance to plug-in developers called, Writing a Plug-in. Convenient isn't it?

Following the instructions provided, we can start creating our plug-in PHP file by specifying our header information. This information allows our plug-in to be recognized and registered within the WordPress environment.

/*
Plugin Name: MU Meta Tags
Plugin URI: http://coyotesdesigns.com
Description: MU Meta Tags adds support for defining unique meta tags
for themes referenced by multiple web sites. This is often the case
with web sites running on the MU framework where more than one site
reference exists to the same theme.
Author: Coyote
Author URI: http://coyotesdesigns.com
Version: 0.9
*/

The next step in this process is to utilize the WordPress Options Mechanism to handle our meta tag data. We will be following the procedures for version 2.7. These procedures are required now that version 2.8 is out, also, µ is running 2.7.1 at the time of this writing.

Within our mu_metatags.php file, we need to hook into the admin menu, like so,

if ( is_admin() ) {
    add_action( 'admin_menu', 'mu_meta_menu' );
    add_action( 'admin_init', 'register_mu_meta_settings' );
} else {
}

function mu_meta_menu() {
    add_options_page( 'Meta Tags', 'Meta Tags', 10,
                       'mu-meta-group', 'meta_options_page' );
}

function register_mu_meta_settings() {
    register_setting( 'mu-meta-group', 'verify-v1' );
    register_setting( 'mu-meta-group', 'meta_keywords' );
    register_setting( 'mu-meta-group', 'meta_description' );
    register_setting( 'mu-meta-group', 'meta_abstract' );
    register_setting( 'mu-meta-group', 'favorite_icon' );
}

For those of you who actually read this far, and are paying attention to the code segment above, you should notice that there is one additional entry that we haven't discussed. That is the inclusion of the favorite_icon in our group. This entry is another unique value per web site. Have you ever noticed the icon in the browser address bar, and when you bookmark a site (like I know you did with this site)? That is what the favorite icon does. It allows you to have an image sent with each browser request that represents your site and/or document. There is once catch to using a favorite icon, though. I'll discuss the options at the end of this article.

I placed the second add_action() in the code above so that you can see the complete reference. Notice that the first add_action() specifies that our Meta Tags menu entry defined in the function, mu_meta_menu, should be included as an option under the Settings menu.

The first statement within the is_admin() test hooks our plug-in into the WordPress admin menu system. Specifically, it will be appended to the Settings menu, in alphabetical order.


Now that we have our menu entry defined, we need a way to accept our data input and subsequently display the data we previously entered for editing later, if necessary. This is accomplished with a standard HTML form. Pay close attention to the class definitions for this form. These allow WordPress to display our form in the same manner as any other option page.

<div class="wrap">
<h2>MU Meta Tags</h2>
<p>Here you define the meta tags values that will be
placed within the theme header.</p>
<form method="post" action="options.php">
// This function does the registration of our options for us
<?php settings_fields( 'mu-meta-group' ); ?>

<table class="form-table">
<tr valign="top">
<th scope="row">Google meta tag verification code</th>
<td><input type="text" name="verify-v1" size="64"
value="<?php echo get_option('verify-v1'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Document Keyword List</th>
<td><input type="text" name="meta_keywords" size="64"
value="<?php echo get_option('meta_keywords'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Document Description</th>
<td><input type="text" name="meta_description" size="64"
value="<?php echo get_option('meta_description'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Document Abstract</th>
<td><input type="text" name="meta_abstract" size="64"
value="<?php echo get_option('meta_abstract'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Favorite Icon URL</th>
<td><input type="text" name="favorite_icon" size="64"
value="<?php echo get_option('favorite_icon'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary"
value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>

This form is not complete, though. We need to wrap the form in the function we defined earlier in the add_options_page function as the response to clicking our menu entry.

// Place this segment before the form
function meta_options_page() {
?>

// Place this segment after the form
<?php
}
?>

Remember that cool little gizmo usually included in the header.php file? This next section is where we will take advantage of this feature.

Pages: 1 2 3

Leave a Reply

You must be logged in to post a comment.


Popular Posts

Sponsor Ad Management User Guide

The cd_ad_sponsor plug-in was written to manage the sponsor ads that can be presented with

A WordPress Sponsor Ad Management Plug-In

I wrote the WordPress Sponsor Ad Management Plug-in to use within the WordPress and WordPr

Quote of the day plugin

The Quote Of The Day plug-in manages quotes displayed on your WordPress or WordPressµ

Quote Of The Day
Finding the occasional straw of truth awash in a great ocean of confusion and bamboozle requires intelligence, vigilance, dedication and courage. But if we don't practice these tough habits of thought, we cannot hope to solve the truly serious problems that face us -- and we risk becoming a nation of suckers, up for grabs by the next charlatan who comes along.
~Carl Sagan