How To Get The Root Page ID In WordPress
The following snippet will let you get the root page id in WordPress.
1 |
<?php $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?> |
Snippet Source/Credit: WordPress.org
How To Have Browser Detection Function In WordPress
Adding the following snippet in your theme’s functions.php file will let you have browser detection function in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
/*Browser Detect*/ function browser_detect(){ $ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod"); $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone"); $firefox = stripos($_SERVER['HTTP_USER_AGENT'],"Firefox"); $safari = stripos($_SERVER['HTTP_USER_AGENT'],"Safari"); $ie = stripos($_SERVER['HTTP_USER_AGENT'],"MSIE"); $opera = stripos($_SERVER['HTTP_USER_AGENT'],"Opera"); $maxthon = stripos($_SERVER['HTTP_USER_AGENT'],"Maxthon"); $chrome = stripos($_SERVER['HTTP_USER_AGENT'],"Chrome"); $netscape = stripos($_SERVER['HTTP_USER_AGENT'],"Netscape"); $traveler = stripos($_SERVER['HTTP_USER_AGENT'], "Traveler"); //detecting device if ($ipod == true || $iphone == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/iphone.png\">"; echo "<br />"; echo "<p>You are viewing this site on an Ipod or Iphone</p>"; echo "</div>"; }elseif($firefox == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Firefox.png\">"; echo "<br />"; echo "<p>You are viewing this site in Firefox</p>"; echo "</div>"; }elseif($ie == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/IE.png\">"; echo "<br />"; echo "<p>You are viewing this site in Internet Explorer</p>"; echo "</div>"; }elseif($netscape == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Netcape.png\">"; echo "<br />"; echo "<p>You are viewing this site in Netscape</p>"; echo "</div>"; }elseif($traveler == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Traveler.png\">"; echo "<br />"; echo "<p>You are viewing this site in Traveler</p>"; echo "</div>"; }elseif($maxthon == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Maxthon.png\">"; echo "<br />"; echo "<p>You are viewing this site in Maxthon</p>"; echo "</div>"; }elseif($chrome == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Chrome.png\">"; echo "<br />"; echo "<p>You are viewing this site in Chrome</p>"; echo "</div>"; }elseif($safari == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Safari.png\">"; echo "<br />"; echo "<p>You are viewing this site in Safari</p>"; echo "</div>"; }elseif($opera == true){ echo "<div class=\"browser\">"; echo "<img src=\"images/Opera.png\">"; echo "<br />"; echo "<p>You are viewing this site in Opera</p>"; echo "</div>"; }else{ echo "<div class=\"browser\">"; echo "<img src=\"images/Q.png\">"; echo "<br />"; echo "<p>We currently dont know your browser.</p>"; echo "</div>"; } }; |
Snippet Source/Credit: afj176.com
How To Have Custom CSS Per Post In WordPress
One-to-many times we do wish to have custom styling in each and every post in WordPress. While such thing didn’t come in-built but with the help of snippet below, one can achieve that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function artStyle() { global $post; if (is_single()) { $currentID = $post->ID; $serverfilepath = TEMPLATEPATH.'/art-direction/style-'.$currentID.'.css'; $publicfilepath = get_bloginfo('template_url'); $publicfilepath .= '/art-direction/style-'.$currentID.'.css'; if (file_exists($serverfilepath)) { echo "<link rel='stylesheet' type=text/css' href='$publicfilepath' media='screen' />"."\n"; } } } add_action('wp_head', 'artStyle'); |
Snippet Source/Credit: digwp.com
How To Have WordPress Login Outside Blog Folder
The following snippet will let you have WordPress login outside blog folder.
1 2 3 4 5 6 7 8 9 |
require_once('../blog/wp-blog-header.php'); if (is_user_logged_in()){ global $user_identity;get_currentuserinfo(); $user = $user_identity; } |
Snippet Source/Credit: Snipplr