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



Inserting Meta Tags

Our plug-in is almost complete. We have one final task to perform. We need to tell WordPress that we wish to use the wp_head() function to allow us to insert our meta tag data. WordPress has a built-in function called, add_action(), that allows us to register our function as part of the pool of functions that execute whenever the wp_head() function is encountered.

add_action('wp_head', 'mu_metadata');

This piece of code tells WordPress that we want to add our function mu_metadata to the list of actions assigned to the wp_head pool.

Finally, we add the code to insert our stored meta tag data.

function mu_metadata() {
	echo "<link rel='shortcut icon'
		type='image/x-icon'
		href='". get_option('favorite_icon')."' />\r\n";
	echo "<link rel='icon'
		href='". get_option('favorite_icon')."'
		type='image/x-icon' />\r\n";
	echo "<meta name='keywords'
		content='". get_option('meta_keywords')."' />\r\n";
	echo "<meta name='description'
		content='". get_option('meta_description')."' />\r\n";
	echo "<meta name='abstract'
		content='". get_option('meta_abstract')."' />\r\n";
	echo "<meta name='verify-v1'
		content='".get_option('verify-v1')."' />\r\n\r\n";
}

This section of code simply uses the PHP echo command, which outputs our meta tag string embedded with our data. We use the get_option() function built in to WordPress to retrieve our stored data and insert that data at the location of the get_option() function. I add the carriage return and new line at the end of the string so that the HTML source does not run on one single line.

Installation

Installation is straight forward, as there are no special files, and no unique configuration settings.

  1. Download the plug-in from the link below.
  2. Extract the files to a local directory.
  3. Upload the files to your plugins directory on your server.
  4. Log in to your WordPress Admin.
  5. Select Plugins from the Admin menu.
  6. Scroll until you find the mu-metatags plug-in listed.
  7. Select Activate, or, if you are using WordPressµ, click Activate Site Wide.

After you activate the plug-in, you will find our menu option under the Settings menu. Clicking the MU Meta Tags option displays our data entry form. Simply add the data you want to associate with each meta tag and click Save Settings.


Using mu-metatags

WordPressµ keeps track of each web site in the database by prefixing the table prefix value, then the site ID to the options table name.

Table 3. Options Table Naming Convention.
Single Site WordPressµ
wp_options wp_n_options
Where n is the site ID.

You need to select a specific site first, then you can click MU Meta Tags from the settings menu to display the data entry form.

If you elected to include the favorite icon option in this plug-in, then you should be aware of the following.

A favorite icon must be named, favicon.ico. See the problem here? WordPress uses the current directory structure for all sites. There can only be one file with a given file name at one time in the same location. The soution is to create unique locations for each favicon.ico file. The practice I prefer is the following:


http://domain.tld/images/sitename/

Using this structure allows for each favicon.ico file to be placed in its own sub-directory. This URL is what you specify for the value for the Favorite Icon URL setting. You can use whatever structure best fits your practices and standards, as long as you ensure a unique location for each icon file.

Conclusion

This article is the first in my series on how to write plug-ins. The next article will expand on this plug-in by modifying our data entry form and allowing for automatic updates. WordPress is a great platform. They took the time to plan how we could enhance the functionality by including our own custom plug-ins. The popularity of WordPress ensures that your plug-ins will have a long lifespan.

I hope that this tutorial was of benefit to you. Please feel free to comment or make suggestions for future articles.


 

Pages: 1 2 3

Leave a Reply

You must be logged in to post a comment.


Popular Posts

Quote of the day plugin

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

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
There is two types of larceny, Petty and Grand. They are supposed to be the same in the eyes of the law, but judges always put a little extra on you for Petty, which is kind of a fine for stupidness.
~Will Rogers