<p>One of the first things I do when I am creating the markup for a new site, is to set a body id. You may wonder what I mean when I say “set a body id”. What I mean is to dynamically check the url to figure out which section you are in. This tutorial will show how to do this dynamically using PHP, and also how to integrate it into WordPress.</p>

<h2>Why Should I Set a Body Id?</h2>
<p>There are plenty of reasons why you should, here are a few:</p>
<ul><li>Maybe your design has different colors depending upon which section you are in. Instead of having to touch the markup, you can just modify the CSS depending upon the id on the <code>body</code>.</li>
<li>You can control the current states for your navigation.</li>
<li>Maybe the markup on your homepage is a little different than subpages, so instead of creating a different template, just stick in an if statement.</li>
</ul><h2>Setting Up the Code</h2>
<p>So typically, you setup your navigation to match your directory structure. Ex: Top level navigation is <strong>about</strong>, and there is a folder on the root called <strong>about</strong>. So the easiest thing to do, is just check the url and determine which section you are in. This is the code that sets the body id on my site:</p>

<div class="codeblock"><code>
&lt;?php<br />function setBodyId() {<br />    $path = $_SERVER['REQUEST_URI'];<br />    <br />    if(!isset($bodyId)) {<br />  if(eregi('^/about/',$path) == 1) {<br />   $bodyId = 'about';<br />  } else if(eregi('^/blog/',$path) == 1) {<br />   $bodyId = 'blog';<br />  } else if(eregi('^/contact/',$path) == 1) {<br />   $bodyId = 'contact';<br />  } else if(eregi('^/work/',$path) == 1) {<br />   $bodyId = 'work';<br />  } else if(eregi('^/play/',$path) == 1) {<br />   $bodyId = 'play';<br />  } else if ($path == '') {<br />   $bodyId = 'home';<br />  } else {<br />   $bodyId = 'general';<br />  }<br /> }<br />   <br />    return $bodyId;<br />}<br />?&gt; 

</code></div>

<p>Sure, you could do this dynamically and pull in the top level folder from the url, but I have found there are too many situations that arise where you want multiple sections to have the same body id, or you want to set other variables section by section. So I stick with this method, and it has been successful.</p>

<h2>My Example</h2>
<p>My example is going to be a little different because it is in a subsection of my site. So here is the code I am using to control setting the body id in the example:</p>
<div class="codeblock"><code>
&lt;?php<br />function exampleSetBodyId() {<br />    $path = $_SERVER['REQUEST_URI'];<br />    <br />    if(!isset($bodyId)) {<br />  if(eregi('^/play/set-a-body-id/about/',$path) == 1) {<br />   $bodyId = 'about';<br />  } else if(eregi('^/play/set-a-body-id/blog/',$path) == 1) {<br />   $bodyId = 'blog';<br />  } else if(eregi('^/play/set-a-body-id/contact/',$path) == 1) {<br />   $bodyId = 'contact';<br />  } else if(eregi('^/play/set-a-body-id/work/',$path) == 1) {<br />   $bodyId = 'work';<br />  } else if(eregi('^/play/set-a-body-id/play/',$path) == 1) {<br />   $bodyId = 'play';<br />  } else if ($path == '/play/set-a-body-id/') {<br />   $bodyId = 'home';<br />  } else {<br />   $bodyId = 'general';<br />  }<br /> }<br />   <br />    return $bodyId;<br />}<br />$bodyId = exampleSetBodyId();<br />?&gt; 

</code></div>

<p>So in the <code>$bodyId</code> variable, you have the value of the body id, so just print the variable on the <code>body</code> tag:</p>

<div class="codeblock"><code>
&lt;body id="&lt;?=$bodyId; ?&gt;"&gt; 

</code></div>

<h3>Some Practical Uses</h3>
<p>As I discussed at the beginning of the post, there are a lot of benefits to having a body id set on every page. This is the <a href="//assets.trevor-davis.com/play/set-a-body-id/">demo section</a> that I am discussing below.</p>
<h4>Different Colors Per Section</h4>
<p>If you click through the navigation in the example, you can see the colors are different for each section. This can be easily accomplished without changing any markup through the power of <strong>descendant selectors.</strong> Here is the part of the CSS that is controlling the color of the borders:</p>
<div class="codeblock"><code>
div#content { border: 1px solid #000; }<br />ul#nav { border-top: 1px solid #000; }<br />body#home div#content, body#home ul#nav { border-color: #FF6600; }<br />body#about div#content, body#about ul#nav { border-color: #429C19; }<br />body#blog div#content, body#blog ul#nav { border-color: #54D0ED; }<br />body#work div#content, body#work ul#nav { border-color: #A81919; }<br />body#play div#content, body#play ul#nav { border-color: #400152; }<br />body#contact div#content, body#contact ul#nav { border-color: #F8DC0C; } 

</code></div>

<p>Here is the part that is changing the color of the <code>h1</code>:</p>

<div class="codeblock"><code>
body#home h1 { color: #FF6600; }<br />body#about h1, { color: #429C19; }<br />body#blog h1 { color: #54D0ED; }<br />body#work h1 { color: #A81919; }<br />body#play h1 { color: #400152; }<br />body#contact h1 { color: #F8DC0C; }&lt;/pre&gt;&lt;/code&gt;<br /><br />&lt;p&gt;Obviously this is not the most beautiful example, but hopefully the idea makes sense.&lt;/p&gt;<br /><br />&lt;h4&gt;Current State on Navigation&lt;/h4&gt;<br />&lt;p&gt;There are different ways to set the current state on the navigation, but by assigning an id to each navigation element, you can use descendant selectors again. Here is the markup:&lt;/p&gt;<br />[code]&lt;ul id="nav"&gt;<br /> &lt;li id="homeNav"&gt;&lt;a href="/play/set-a-body-id/"&gt;Home&lt;/a&gt;&lt;/li&gt;<br /> &lt;li id="aboutNav"&gt;&lt;a href="/play/set-a-body-id/about/"&gt;About&lt;/a&gt;&lt;/li&gt;<br /> &lt;li id="blogNav"&gt;&lt;a href="/play/set-a-body-id/blog/"&gt;Blog&lt;/a&gt;&lt;/li&gt;<br /> &lt;li id="workNav"&gt;&lt;a href="/play/set-a-body-id/work/"&gt;Work&lt;/a&gt;&lt;/li&gt;<br /> &lt;li id="playNav"&gt;&lt;a href="/play/set-a-body-id/play/"&gt;Play&lt;/a&gt;&lt;/li&gt;<br /> &lt;li id="contactNav"&gt;&lt;a href="/play/set-a-body-id/contact/"&gt;Contact&lt;/a&gt;&lt;/li&gt;<br />&lt;/ul&gt; 

</code></div>

<p>Then, we use the following CSS to change the background color and text color when we are in that section:</p>
<div class="codeblock"><code>
body#home ul#nav li#homeNav a { background: #FF6600; }<br />body#about ul#nav li#aboutNav a { background: #429C19; }<br />body#blog ul#nav li#blogNav a { background: #54D0ED; }<br />body#work ul#nav li#workNav a { background: #A81919; }<br />body#play ul#nav li#playNav a { background: #400152; }<br />body#contact ul#nav li#contactNav a { background: #F8DC0C; }<br />body#home ul#nav li#homeNav a, body#about ul#nav li#aboutNav a, body#blog ul#nav li#blogNav a, body#contact ul#nav li#contactNav a { color: #000; }<br />body#work ul#nav li#workNav a, body#play ul#nav li#playNav a { color: #fff; } 

</code></div>

<h4>Slightly Different Homepage Markup</h4>
<p>Typically the markup for homepages are slightly different. But, if you are using one header and footer template for the entire site, you probably don’t want to create another template just for the homepage. So since we have the body id that tells us when we are on the homepage, we can just write a simple <code>if</code> statement:</p>

<div class="codeblock"><code>
&lt;?php if($bodyId == 'home') { ?&gt;<br /> &amp;hellip;PUT YOUR HOMEPAGE SPECIFIC ELEMENTS HERE&amp;hellip; <br />&lt;?php } ?&gt; 

</code></div>

<h2>Make it Work with WordPress</h2>
<p>So just take the <strong>setBodyId function</strong> that we defined earlier, and add it to your WordPress theme functions (functions.php).</p>

<p>After you have added the function, we want to add actions to <a href="http://codex.wordpress.org/Hook_Reference/wp_head">wp_head</a> and <strong>wp_footer</strong> (for some reason there isn’t a WordPress Hook Reference for wp_footer, but it is essentially the same as wp_head, just in the footer). Here is how we add our function to those hooks:</p>
<div class="codeblock"><code>
add_action('wp_head', 'setBodyId');<br />add_action('wp_footer', 'setBodyId'); 

</code></div>

<p>Then, in your header, just call the <strong>wp_head</strong> hook, and call our function:</p>
<div class="codeblock"><code>
&lt;?php wp_head(); $bodyId = setBodyId(); ?&gt; 

</code></div>

<p>There you have it, you have the body id defined in the <code>$bodyId</code> variable, and you can do with it what you want.</p>

<h2>Other Thoughts</h2>
<p>I think by setting an id (or class) on such a high level element gives you a lot of control over its descendants. Another interesting idea that I heard was to use <a href="http://jquery.com/">jQuery</a> to determine the browser and version that the user is using, then add a class on the body. That way you could differ your CSS depending on which class is on the <code>body</code>. That idea would eliminate the need for conditional comments (at least for users with JavaScript enabled).</p>

<p>So hopefully this tutorial shows you an easy way to make some site-wide tasks simpler, and I am interested to see if other people have ideas for what to do with body ids.</p>