Late night basketball? I'm in.

Dynamically Set a Body Id in PHP

I have already written a couple of articles about body ids:
Set a Body Id Using PHP
and Setting Body Ids in Expression Engine, but in a recent project, I was looking for a more dynamic way to do it in PHP and WordPress.

I don’t think it’s necessary to discuss the benefits of body ids because I have already discussed them in the other posts. So if you still aren’t using body ids/classes, go read the articles, and start using them.

The Plan

This seems so straightforward that I’m not sure why I didn’t start with this method. So basically, I look at the URI, and if it is equal to / then I give it a body id of home. Otherwise, I grab the first “folder” and set that as the body id.

Example

Let’s say that the URI is: /about/team/leadership/. The body id would be about. Ok, the seems easy, let’s get to the code.

The Code

Here is our setBodyId function:

function setBodyId() {
	$path = $_SERVER['REQUEST_URI'];

	if($path === '/') {
		$bodyId = 'home';
	} else {
		$bodyId = substr($path, 1, (strpos($path,'/',1)-1));
	}

	return $bodyId;
}

If you are using this in WordPress, put this function in your theme function.php file.

Then, you want to call the function and assign it to a variable:

$bodyId = setBodyId();

Finally, if the $bodyId variable is not empty, add the id attribute on the body tag:

<body<?php if(!empty($bodyId)) echo ' id="'.$bodyId.'"';?>>

Conclusion

It’s so simple, yet powerful. This could also easily be recoded in other programming languages. What do you think? Any ideas for improvement?

Share This:
  • del.icio.us
  • Digg
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • NewsVine
  • Technorati
  • Reddit
  • LinkedIn

RSS feed of comments for this entry

  1. Nice method. but why not shorten the code inside the body tag to just code call like most other wordpress function calls are instead of setting it to the you should never get an empty unless your uri was never passed, but you can easily fix that by changing the if else insides around so that even if your is messed up you get an id of home no matter what. currently it’ll give you nothing if for some reason the uri is never passed. And echo instead of return. This shortens the code in the body tag to just

    <body id=”">

  2. grr code didn’t pass through but there should be just a php function call inside the id tag.

  3. @Alicia-
    True, that would work too.

    One reason I like to set a variable is because I sometimes will use the $bodyId variable in conditional statements when determining what to show on a page. Sometimes it can be a little shorter than using the WordPress conditional statements.

  4. Thanks, exactly what I’ve been looking for! :D

  5. It appears good. It is working well in my site.

  6. Thanks for sharing. Works beautifully and standards compliant.

    • 4.3.2009 at 7:24 pm
    • #

Speak Your Mind

* Denotes Required Field

  1. To post code snippets, use <pre><code>YOUR CODE HERE</code></pre>
Me, Trevor Davis. My blue steel face…

Hi, I’m Trevor Davis

I’m a 25 year old Front-End Developer living in Arlington, VA. I work full-time at Viget Labs and freelance on the side.

I specialize in CSS, HTML, jQuery, WordPress & ExpressionEngine. See more of my work, and then hire me.

Recent Work

  • 4th District IBEW Health Fund
  • Employers Council on Flexible Compensation
  • Monica Davis
  • The National Christmas Tree
  • Matrix Group International
  • The MatriX Files
  • Wireless Career
  • George Washington Wired
  • Direct Selling 411
  • Makeup Bizz
  • InstallNET
  • National Park Foundation
See More of My Work

Asides