Guest User

Untitled

a guest
May 13th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: SEO Optimized Images
  5. Plugin URI: http://webriti.com
  6. Description: The **SEO Optimized Images** plugin Seo Optmized Images Plugin lets you dynamically insert Seo Friendly alt attributes and title attributes to your Images . Simply activate the plugin, provide the pattern and you are ready to go.
  7. Version: 1.3.1
  8. Author: priyanshu.mittal
  9. Author URI: http://webriti.com
  10.  
  11.  
  12. */
  13. add_filter('the_content', 'soi_replace_tags', 100);
  14. // add_filter();
  15.  
  16.  
  17. function soi_replace_tags ($content, $alt_text='',$title='')
  18. {
  19.  
  20.  
  21. global $post;
  22.  
  23. $soi_options_array = get_option('soi_options_values');
  24.  
  25. $alt_text = $soi_options_array['soi_alt_value'];
  26. $title_text = $soi_options_array['soi_title_value'];
  27.  
  28. // get the post title for later use
  29. $post_title = esc_attr($post->post_title);
  30.  
  31. // preapre the alt text
  32.  
  33.  
  34. // Check if we need to overide the default alt and existing alt text
  35. // We will set the flag 1 or 0
  36.  
  37. //check setting for overinding alt tag
  38. $alt_flag = $soi_options_array['soi_override_alt_value'];
  39.  
  40. //check setting for overinding title tag
  41. $title_flag = $soi_options_array['soi_override_title_value'];
  42.  
  43. // Set the alt pattern
  44.  
  45.  
  46.  
  47.  
  48. // print_r($post);
  49.  
  50.  
  51.  
  52. // This piece of code first finds all the images in the page
  53. // Then we proceed to finding missing or empty alt tags
  54.  
  55. $soi_options_array = get_option('soi_options_values');
  56.  
  57. // count number of images found in content
  58. $count = preg_match_all('/<img[^>]+>/i', $content, $images);
  59.  
  60.  
  61. // If we find images on the page then proceed to check the alt tags
  62.  
  63. // We also need to calaculate the velue to be inserted in the tags based on user input
  64.  
  65.  
  66. if($count>0)
  67. {
  68.  
  69. // Here we will set the alt value to be inserted.
  70. // $t = "$post_title"
  71. // we want to output like alt = "text"
  72.  
  73. $t = 'alt = "'.$alt_text.'"';
  74.  
  75. // we want to output like title = "text"
  76. $t_title = 'title = "'.$title_text.'"';
  77.  
  78.  
  79. foreach($images[0] as $img)
  80. { // check if the alt tag exists in the image
  81.  
  82.  
  83. // Get the Name of Image Files.
  84.  
  85. $output = preg_match_all( '/<img[^>]+src=[\'"]([^\'"]+)[\'"].*>/i', $img, $matches);
  86.  
  87. $get_file_name = pathinfo($matches[1][0]);
  88. $image_file_name = $get_file_name['filename'];
  89.  
  90.  
  91. // Get post categories
  92. $postcategories = get_the_category();
  93. $post_category='';
  94. if ($postcategories) {
  95. foreach($postcategories as $category) {
  96. $post_category .= $category->name .' ';
  97. }
  98. }
  99.  
  100. /// fetch the values of alt and title tags from the option panel
  101. $alt_text = $soi_options_array['soi_alt_value'];
  102. $title_text = $soi_options_array['soi_title_value'];
  103.  
  104. // Replace the Values for alt tag
  105.  
  106. $alt_text = str_replace('%title',$post_title,$alt_text );
  107. $alt_text = str_replace('%name',$image_file_name,$alt_text );
  108. $alt_text = str_replace('%category',$post_category,$alt_text );
  109.  
  110. // replace the values for title tag.
  111. $title_text = str_replace('%title',$post_title,$title_text );
  112. $title_text = str_replace('%name',$image_file_name,$title_text );
  113. $title_text = str_replace('%category',$post_category,$title_text );
  114.  
  115. //configure tags with specified values from option panel.
  116. $t = 'alt = "'.$alt_text.'"';
  117. $t_title = 'title = "'.$title_text.'"';
  118.  
  119. //take the alt tag out from the image html markup
  120. $is_alt = preg_match_all('/alt="([^"]*)"/i', $img, $alt);
  121.  
  122.  
  123.  
  124. ////////////////// check for alt tag /////////////////////////
  125. // In case there is not alt tag, create the tag and insert the value
  126. if ($alt_flag == "1")
  127.  
  128. {
  129. // if alt tag is not present than insert the tag.
  130. if($is_alt == 0)
  131. { $new_img = str_replace('<img ', '<img '.$t , $img);
  132. $content = str_replace($img, $new_img, $content);
  133. }
  134.  
  135. // if alt tag is present
  136. elseif($is_alt==1)
  137.  
  138. {
  139.  
  140. $text = trim($alt[1][0]);
  141.  
  142.  
  143. // Check if the alt text is empty.
  144.  
  145. if(empty($text))
  146. {
  147.  
  148.  
  149. $new_img = str_replace($alt[0][0], $t, $img);
  150.  
  151. $content = str_replace($img, $new_img, $content);
  152. }
  153.  
  154.  
  155.  
  156.  
  157. // Should we override the existing alt tag
  158. if ($alt_flag == "1")
  159.  
  160. {
  161.  
  162. $new_img = str_replace($alt[0][0], $t, $img);
  163.  
  164. $content = str_replace($img, $new_img, $content);
  165.  
  166.  
  167. }
  168.  
  169. }
  170. }//////////////////// checked for alt tag ////////////////////
  171.  
  172. ///////////////// check for title tag ///////////////////////////
  173.  
  174.  
  175. // first check weither title tag needs to be overide
  176. if($title_flag == "1"){
  177.  
  178. if(!isset($new_img)) $new_img=$img; // when alt tag is not overridden, than , use actual image markup ie $new_img.
  179.  
  180. $is_title = preg_match_all('/title="([^"]*)"/i', $new_img, $title);
  181.  
  182. // check if title tag is not present in the img tag
  183. if($is_title == 0)
  184. {
  185. // create the title tag and insert the tag
  186. $final_img = str_replace('<img ', '<img '.$t_title , $new_img);
  187. $content = str_replace($new_img, $final_img, $content);
  188.  
  189. } else {
  190.  
  191. // you are here bcs title tags exsis and needs to be override
  192. $final_img = str_replace($title[0][0], $t_title, $new_img);
  193. $content = str_replace($new_img, $final_img, $content);
  194. }
  195. }
  196. ///////////////////// title tag checked ////////////////
  197.  
  198. }
  199. }
  200.  
  201. return $content;
  202. }
  203.  
  204.  
  205.  
  206. add_action('admin_menu', 'soi_add_menu_page');
  207.  
  208. function soi_add_menu_page()
  209. {
  210. add_menu_page( 'soi_settings_page', 'Seo Optimized Images', 'administrator', 'soi_setting','soi_create_setting_page','');
  211. }
  212.  
  213. function soi_create_setting_page()
  214. {
  215. require_once('seo-optimized-images-settings.php');
  216. }
  217.  
  218.  
  219. add_action( 'admin_enqueue_scripts', 'soi_load_custom_wp_admin_style' );
  220.  
  221. function soi_load_custom_wp_admin_style($hook) {
  222. if ($hook != 'toplevel_page_soi_setting'){return;} // we dont want to load our css on other pages
  223. wp_register_style ('soi_custom_wp_admin_css', plugins_url('css/plugin-admin-panel.css', __FILE__));
  224. wp_enqueue_style( 'soi_custom_wp_admin_css' );
  225. wp_enqueue_style( 'wp-color-picker' ); // here we add the color picker style for use in our plugin
  226.  
  227. }
  228.  
  229.  
  230.  
  231. function soi_load_custom_wp_admin_scripts($hook) {
  232.  
  233.  
  234. if ($hook != 'toplevel_page_soi_setting'){return;} // we dont want to load our js on other pages
  235.  
  236. wp_register_script( 'soi_custom_wp_admin_js', plugin_dir_url( __FILE__ ) . 'js/plugin-admin-panel.js',array('jquery','jquery-ui-core','jquery-ui-tabs','wp-color-picker'), false, '1.0.0' );
  237. wp_enqueue_script ('soi_custom_wp_admin_js');
  238.  
  239.  
  240. }
  241. add_action( 'admin_enqueue_scripts', 'soi_load_custom_wp_admin_scripts' );
Add Comment
Please, Sign In to add comment