Simple wordpress seo plugin using custom meta – Final Part

This post is a continuation of my previous tutorial Simple wordpress seo plugin using custom meta – Part 1

In the previous part, we had finished creating Meta data forms and saving the Meta data. So we will continue with creating an Options page for our plugin and inserting our custom meta data in to the head section.

seo-options-page

Add an Options Page

add_action('admin_menu', 'seo_global_options');
function seo_global_options() {
   add_options_page('SEO options', 'SEO options', 'manage_options', 'seo_global_options', 'seo_options_page');
}

function seo_options_page() {

<div class="wrap">
<h2>Simple SEO options page</h2>

<form action="" method="post"><!--?php wp_nonce_field( 'update_seo_options', 'seo_options_nonce' ); ?--> 
<input type="radio" name="auto_enable_seo" value="1" < ?php if(get_option('auto_enable_seo')) echo 'checked="checked"'; ?> /> 
<label>Automatically insert meta data<label>
<input type="radio" name="auto_enable_seo" value="0" < ?php if(get_option('auto_enable_seo') == 0) echo 'checked="checked"'; ?> />
<label>No, to insert manually.<label>

<table class="form-table">
	<tbody>
		<tr valign="top">
			<th scope="row">Enable seo title</th>
			<td><input type="checkbox" name="enable_seo_title" < ?php if(get_option('enable_seo_title')) echo 'checked="checked"'; ?> value="yes" /> 
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><strong>Homepage</strong></th>
			<td></td>
		</tr>
		<tr valign="top">
			<th scope="row">Title</th>
			<td><input type="text" name="home_title" class="regular-text" value="< ?php echo get_option('home_title'); ?>" /></td>
		</tr>
		<tr valign="top">
			<th scope="row">Description</th>
			<td><textarea name="home_desc" class="large-text">< ?php echo get_option('home_desc'); ?></textarea></td>
		</tr>
		<tr valign="top">
			<th scope="row">Keywords</th>
			<td><input type="text" name="home_keywords" class="regular-text" value="< ?php echo get_option('home_keywords'); ?>" /></td>
		</tr>
		<tr valign="top">
			<th scope="row">Meta robot</th>
			<td>
			<select name="home_meta_robot">
			 < ?php $options_array = array('index,follow', 'noindex,follow', 'noindex,nofollow', 'all');
				foreach($options_array as $op_value) {
					$is_selected = ($op_value == get_option('home_meta_robot')) ? 'selected="selected"' : '';
					echo '<option value="'.$op_value.'" '.$is_selected.'>'.$op_value.'</option>';
				}
			  ?>
			</select>
			</td>
		</tr>
	</tbody>
</table>

<input type="hidden" name="update_seo_options" value="abx" /> 
<input class="button-primary" type="submit" name="submit" value="<?php _e('Save Changes') ?>" />

</form></div>
<?php }

As you see in the code, we have built an option to auto insert the meta data in to the head section. And we have a checkbox option to enable or disable the seo title. Also we have some inputs to save separate meta title, meta keywords, meta description and meta robot for the home page.

Save the options page’s data

if(isset($_POST['update_seo_options']) && $_POST['update_seo_options'] == 'abx' ) {
	update_option('auto_enable_seo', trim($_POST['auto_enable_seo']));
	update_option('enable_seo_title', trim($_POST['enable_seo_title']));
	update_option('home_title', trim($_POST['home_title']));
	update_option('home_desc', trim($_POST['home_desc']));
	update_option('home_keywords', trim($_POST['home_keywords']));
	update_option('home_meta_robot', trim($_POST['home_meta_robot']));
}

Insert the meta data in to head section

So we are in the final step of building the plugin. First, we will replace the page title with our custom meta title using add_filter function.

if(get_option('enable_seo_title')) add_filter('wp_title', 'home_title');
function home_title($orig_title) { 
	if(is_home() || is_front_page()) {
		$seo_title = get_option('home_title');
		return $seo_title;  
	 }
	 elseif(is_single() || is_page()) {
		$meta_data = get_post_custom($post->ID);
		$seo_title = $meta_data["seo_title"][0];
		return $seo_title;
	}
	else return $orig_title;
}

And now we will insert the other meta data in to the header section.

if(get_option('auto_enable_seo')) add_action('wp_head', 'tg_custom_meta');
function tg_custom_meta() {
	global $post;
	$meta_data = get_post_custom($post->ID);

 	if($meta_data["enable_seo_title"][0]) echo '<title>'. $meta_data["seo_title"][0] .'</title>';
	echo '<meta name="keywords" content="'. $meta_data["seo_keywords"][0] .'" />';
	echo '<meta name="description" content="'. $meta_data["seo_description"][0] .'" />';
	echo '<meta name="robots" content="'. $meta_data["meta_robot"][0] .'" />';
	echo $meta_data["seo_custom"][0];
}

Use this template tag to place the custom meta data in your desired location of the head section.
Note: Don’t forget to select “No, to insert manually” in this case.

< ?php if (function_exists('custom_meta')) tg_custom_meta(); ?>

328 Downloads