Intelligent Menus

Released on November 7, 2003.
Last Updated: November 7, 2003 2:58 PM
Version: 1.5


Using some very simple PHP and CSS you can create intelligent menus that are easy to use and maintain.


Pop your code, one link per line, into the $menu variable and then put the rest wherever you want your menu to appear.


Intelligent Menu Code

$menu = <<<MENU
<ul id="nav">
    <li><a href="/" title="Where the heart is">Home</a></li>
    <li><a href="/archives/" title="Things that have passed.">Archives</a></li>
    <li><a href="/about/" title="All about Matt">About</a></li>
    <li><a href="/photos/" title="It's the 'photo' in Matt">Photos</a></li>
    <li><a href="/music/" title="The food of love">Music</a></li>
    <li><a href="/scripts/" title="Free (as in beer and speech) code">Scripts</a></li>
    <li><a href="/jazzquotes/" title="Great quotes from amazing musicians. More in the future.">Jazz Quotes</a></li>
    <li><a href="/toys/" title="Make me tick: Reviews of various electronics I own or have come into contact with.">Toys</a></li>
    <li><a href="/xml/" title="Syndicate the content here. Get Matt to-go.">Syndicate</a></li>
    <li><a href="/portal/" title="My personal start page, has my blogroll and links to interesting sites">Portal</a></li>
    <li><a href="/zeitgeist/" title="A google of random stats about the website">Zeitgeist</a></li>
    <li><a href="/contact/" title="Reach out and touch somebody. How to get in contact with me, also has GPG key and geek code.">Contact</a></li>

$lines = split("\n", $menu);
foreach ($lines as $line) {
    $current = false;
    preg_match('/href="([^"]+)"/', $line, $url);
    if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
        $line = str_replace('<a h', '<a id="current" h', $line);
    echo $line."\n";

9 replies on “Intelligent Menus”

[…] I finally got around to polishing off the look for my website.  Since I barely know what I’m doing when mucking with WordPress or Gallery2 (or the technologies they are using – mostly CSS) – it took me significantly longer getting a unified look to my site than it should have.  However, when it is all said and done (and it is almost all done), I am very happy with the results.  Now it is your turn to tell me what you think.  Leave a comment to let me know.  Make sure to notice the integration with the Photo Gallery section of the site. I am using a reasonably modified WordPress Dxx theme, along with the Gallery2 WPG2 theme, which has been reasonably heavily modified as well.  On top of these modifications, I added Matt’s Intelligent Menu system, and finally did more CSS mangling to get everything to look reasonably clean. […]

dude seriously… I know this code is like 4 years old but I think it’s one of the most under appreciated little easter eggs I’ve ever seen for WordPress. Hell, not only WordPress but all sites that need some easy-to implement stupid-simple dynamic menus. It also works great for other CMS systems like Expression Engine and such. thanks Matt.

Ever since I started using wordpress, I found this script from matt’s site and implemented it in every menu I have used. It is just that simple and awesome to use.

Wow, the last version I saw in a custom Drupal theme was like 25 lines long. I don’t know why they didn’t use something simpler like your short script.

But why the “$current = false;” line? Did you have this code in a function and forget to remove that?