Skip to main content

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

  1. add_action('admin_menu''seo_global_options');  
  2. function seo_global_options() {  
  3.    add_options_page('SEO options''SEO options''manage_options''seo_global_options''seo_options_page');  
  4. }  
  5.   
  6. function seo_options_page() {  
  7.   
  8. <div class="wrap">  
  9. <h2>Simple SEO options page</h2>  
  10.   
  11. <form action="" method="post"><!--?php wp_nonce_field( 'update_seo_options''seo_options_nonce' ); ?-->  
  12. <input type="radio" name="auto_enable_seo" value="1" < ?php if(get_option('auto_enable_seo')) echo 'checked="checked"'; ?> />  
  13. <label>Automatically insert meta data<label>  
  14. <input type="radio" name="auto_enable_seo" value="0" < ?php if(get_option('auto_enable_seo') == 0) echo 'checked="checked"'; ?> />  
  15. <label>No, to insert manually.<label>  
  16.   
  17. <table class="form-table">  
  18.     <tbody>  
  19.         <tr valign="top">  
  20.             <th scope="row">Enable seo title</th>  
  21.             <td><input type="checkbox" name="enable_seo_title" < ?php if(get_option('enable_seo_title')) echo 'checked="checked"'; ?> value="yes" />  
  22.             </td>  
  23.         </tr>  
  24.         <tr valign="top">  
  25.             <th scope="row"><strong>Homepage</strong></th>  
  26.             <td></td>  
  27.         </tr>  
  28.         <tr valign="top">  
  29.             <th scope="row">Title</th>  
  30.             <td><input type="text" name="home_title" class="regular-text" value="< ?php echo get_option('home_title'); ?>" /></td>  
  31.         </tr>  
  32.         <tr valign="top">  
  33.             <th scope="row">Description</th>  
  34.             <td><textarea name="home_desc" class="large-text">< ?php echo get_option('home_desc'); ?></textarea></td>  
  35.         </tr>  
  36.         <tr valign="top">  
  37.             <th scope="row">Keywords</th>  
  38.             <td><input type="text" name="home_keywords" class="regular-text" value="< ?php echo get_option('home_keywords'); ?>" /></td>  
  39.         </tr>  
  40.         <tr valign="top">  
  41.             <th scope="row">Meta robot</th>  
  42.             <td>  
  43.             <select name="home_meta_robot">  
  44.              < ?php $options_array = array('index,follow''noindex,follow''noindex,nofollow''all');  
  45.                 foreach($options_array as $op_value) {  
  46.                     $is_selected = ($op_value == get_option('home_meta_robot')) ? 'selected="selected"' : '';  
  47.                     echo '<option value="'.$op_value.'" '.$is_selected.'>'.$op_value.'</option>';  
  48.                 }  
  49.               ?>  
  50.             </select>  
  51.             </td>  
  52.         </tr>  
  53.     </tbody>  
  54. </table>  
  55.   
  56. <input type="hidden" name="update_seo_options" value="abx" />  
  57. <input class="button-primary" type="submit" name="submit" value="<?php _e('Save Changes') ?>" />  
  58.   
  59. </form></div>  
  60. <?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

  1. if(isset($_POST['update_seo_options']) && $_POST['update_seo_options'] == 'abx' ) {  
  2.     update_option('auto_enable_seo', trim($_POST['auto_enable_seo']));  
  3.     update_option('enable_seo_title', trim($_POST['enable_seo_title']));  
  4.     update_option('home_title', trim($_POST['home_title']));  
  5.     update_option('home_desc', trim($_POST['home_desc']));  
  6.     update_option('home_keywords', trim($_POST['home_keywords']));  
  7.     update_option('home_meta_robot', trim($_POST['home_meta_robot']));  
  8. }  

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.
  1. if(get_option('enable_seo_title')) add_filter('wp_title''home_title');  
  2. function home_title($orig_title) {  
  3.     if(is_home() || is_front_page()) {  
  4.         $seo_title = get_option('home_title');  
  5.         return $seo_title;  
  6.      }  
  7.      elseif(is_single() || is_page()) {  
  8.         $meta_data = get_post_custom($post->ID);  
  9.         $seo_title = $meta_data["seo_title"][0];  
  10.         return $seo_title;  
  11.     }  
  12.     else return $orig_title;  
  13. }  
And now we will insert the other meta data in to the header section.
  1. if(get_option('auto_enable_seo')) add_action('wp_head''tg_custom_meta');  
  2. function tg_custom_meta() {  
  3.     global $post;  
  4.     $meta_data = get_post_custom($post->ID);  
  5.   
  6.     if($meta_data["enable_seo_title"][0]) echo '<title>'$meta_data["seo_title"][0] .'</title>';  
  7.     echo '<meta name="keywords" content="'$meta_data["seo_keywords"][0] .'" />';  
  8.     echo '<meta name="description" content="'$meta_data["seo_description"][0] .'" />';  
  9.     echo '<meta name="robots" content="'$meta_data["meta_robot"][0] .'" />';  
  10.     echo $meta_data["seo_custom"][0];  
  11. }  
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.
  1. < ?php if (function_exists('custom_meta')) tg_custom_meta(); ?>  
70 Downloads

Comments

Popular posts from this blog

உடல் எடையை குறைக்க வேண்டுமா ?

இன்றைய அவசர உலகின் மிக பெரிய பிரச்சனையாக இருப்பது உடல் எடை அதிகரிப்பது தான்.மனம் போன போக்கில் உணவு கட்டு பாடு இல்லாமல் கண்டதையும் உள்ளே தள்ளுவதும்,உக்காந்த இடத்திலேயே கணணி முன் நேரத்தை விரயமாக்குவதும் தான் இந்த பிரச்சனைக்கு மூல காரணமாகும். அது சரி இந்த பிரச்சனையை எப்படி இல்லாமல் செய்வது அல்லது உடல் எடையை எவ்வாறு குறைப்பது என்பதை பற்றி பாப்போம் , பல வருட ஆரய்சிக்குபின் மருத்துவர்கள்   உடல் எடைய குறைக்க மிகவும் சுலபமான உடற்பயிற்சியை கண்டுபிடித்துள்ளனர்.இது  100% பயனளிக்க கூடியது, எந்த இடத்திலும் எந்தநேரத்திலும் மிக சுலபமா செய்ய கூடிய உடற் பயிற்சியாகும்.இந்த உடற்பயிற்சிகள் படத்துடன் கீழே தரப்பட்டுள்ளது நீங்களும் முயற்சித்து பாருங்கள கண்டிப்பாக பலன் கிடைக்கும்... முதலில் நாற்காலியில் உட்கார்ந்து இட  பக்கம் பார்கவும் .. ..        அடுத்து  நாற்காலியில் உட்கார்ந்து வல  பக்கம் பார்கவும்  ....  நண்பர்கள் யாரவது மச்சி வாடா சின்ன பீஸ் ,இங்க பாரு சூப்பர் அய்டம்னு சொல்லி கால்ல விழுந்து கூப்பிட்டலோ மேற்கூறிய உடற் பயி...

38 (new) web tools to keep you busy

For many of us, the internet represents our daily job, income resource or biggest hobby. Every day we check our emails, read our feeds, visit our websites, find and discuss new things and GOD knows what else. It requires a lot of tools to do all this stuff and sometimes, we forget to search for easier solutions losing valuable time or keeping down the production graph. It's very hard to keep track with everything that's new and popular and this is why we do monthly searches for the best web tools out there. Enjoy!  45+ Web Operating Systems "There are many more web operating systems hoping to bring all your usual desktop applications online in one place - here are more than 45 of our favorites."  15 Ways To Create Website Screenshots "15 Ways To Create Website Screenshots"  Open Source Windows "The promise of open source software is best quality, flexibility and reliability. The only way to have TRUE "Open Source Windows"is...

(Loot again) Get Mi Earphones & Mi Backpack In Just Rs.1

How to Get Mi Band , Mi Earphones & Mi Backpack In Just Rs.1 1, First Of All Just  Click On Below Links 1 By 1  Click 1->  Mi City Backpack Dark Grey Minimalist sleek design  -In Just Rs.1 Click 2->  Mi Earphones Silver Dynamic bass sound  -In Just Rs.1 2. Now Open All Links 1 By 1 3. After Opening The Link Click On “ Click to Bring The Price Down ” Then Click “ I Want it too ” 4. Login into Your Mi Account ( Or Sign Up  –  New Mi Account Giving Huge Price drop Like Rs.50 Or Rs.70 Drop , So Recommend You to Make New Account ) 5. Now You will See 3 Products There In this  Mi 24 Hour Madness Sale  6. Click On “ Participate ” Now Button & Share The Link With Your Friends When Each Friend Click On Your Link , You Will See Some Price cut in Your Products… Finally After Enough Click Your Product Will be Available For Just Rs.1  7. Start Referring , Start Looting MI