WordPress extended body class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // via bit.ly/wpthemg-bodclass
  4.  
  5. /**
  6.  * Extends the body_class(); to include browser detection
  7.  * Props to Thematic: http://wordpress.org/extend/themes/thematic
  8.  */
  9.  
  10. function browser_body_class($classes) {
  11.  
  12. // A little Browser detection shall we?
  13. $browser = $_SERVER[ 'HTTP_USER_AGENT' ];
  14.  
  15. // Mac, PC ...or Linux
  16. if ( preg_match( "/Mac/", $browser ) ){
  17. $classes[] = 'mac';
  18.  
  19. } elseif ( preg_match( "/Windows/", $browser ) ){
  20. $classes[] = 'windows';
  21.  
  22. } elseif ( preg_match( "/Linux/", $browser ) ) {
  23. $classes[] = 'linux';
  24.  
  25. } else {
  26. $classes[] = 'unknown-os';
  27. }
  28.  
  29. // Checks browsers in this order: Chrome, Safari, Opera, MSIE, FF
  30. if ( preg_match( "/Chrome/", $browser ) ) {
  31. $classes[] = 'chrome';
  32.  
  33. preg_match( "/Chrome\/(\d.\d)/si", $browser, $matches);
  34. $classesh_version = 'ch' . str_replace( '.', '-', $matches[1] );
  35. $classes[] = $classesh_version;
  36.  
  37. } elseif ( preg_match( "/Safari/", $browser ) ) {
  38. $classes[] = 'safari';
  39.  
  40. preg_match( "/Version\/(\d.\d)/si", $browser, $matches);
  41. $sf_version = 'sf' . str_replace( '.', '-', $matches[1] );
  42. $classes[] = $sf_version;
  43.  
  44. } elseif ( preg_match( "/Opera/", $browser ) ) {
  45. $classes[] = 'opera';
  46.  
  47. preg_match( "/Opera\/(\d.\d)/si", $browser, $matches);
  48. $op_version = 'op' . str_replace( '.', '-', $matches[1] );
  49. $classes[] = $op_version;
  50.  
  51. } elseif ( preg_match( "/MSIE/", $browser ) ) {
  52. $classes[] = 'msie';
  53.  
  54. if( preg_match( "/MSIE 6.0/", $browser ) ) {
  55. $classes[] = 'ie6';
  56. } elseif ( preg_match( "/MSIE 7.0/", $browser ) ){
  57. $classes[] = 'ie7';
  58. } elseif ( preg_match( "/MSIE 8.0/", $browser ) ){
  59. $classes[] = 'ie8';
  60. } elseif ( preg_match( "/MSIE 9.0/", $browser ) ){
  61. $classes[] = 'ie9';
  62. }
  63.  
  64. } elseif ( preg_match( "/Firefox/", $browser ) && preg_match( "/Gecko/", $browser ) ) {
  65. $classes[] = 'firefox';
  66.  
  67. preg_match( "/Firefox\/(\d)/si", $browser, $matches);
  68. $ff_version = 'ff' . str_replace( '.', '-', $matches[1] );
  69. $classes[] = $ff_version;
  70.  
  71. } else {
  72. $classes[] = 'unknown-browser';
  73. }
  74.  
  75. return $classes;
  76. }
  77.  
  78. add_filter('body_class','browser_body_class');
  79.  
  80. ?>

URL: http://bit.ly/wpthemg-bodclass

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.