Dynamic Menus in PHP

I do not claim to know anything much about PHP, this is really quite a hack on my own part and I’m betting there are better ways to do it (more robust, less code to change, etc). I’d love to hear your comments on this. I’ve just been wanting to do dynamic menus and a common header (at the least) across all my diverse web applications (gallery, blog, wiki, etc).

Brian says: See.. just leave a comment like this! It’s easy. =)


On my brianwiese.net website document root, all pages include:

  • /inc/headtext.php
    • which displays all that goes <head>inside here</head> on all index.php pages
    • provides default CSS themes to make header look good, also inserts:
      • /inc/dynmenu-head.php
        • which adds PHP logic for dynamic menu #id selection
  • /inc/header.php
    • which displays the menu itself, after <body> tag with some custom #id and .class tags added


  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Brian Wiese">
  <meta name="Description" content="brianwiese.net homepage website">
  <meta name="Keywords" content="brian wiese, homepage, website, gnu, linux, geek, nerd, clever, smart, hacker, gnu, linux, peace, love, photos">
  <title>Brian Wiese - Homepage</title>
  <link rel="icon" href="favicon.ico" type="image/x-icon">
  <!-- <link rel="icon" href="logo/lvs-16.png" type="image/png"> -->
  <link rel="home" title="Home" href="index.html">
  <link rel="stylesheet" type="text/css" href="/css/base/content.css" media="all">
  <link rel="stylesheet" type="text/css" href="/css/base/template.css" media="screen">
  <link rel="stylesheet" type="text/css" href="/css/cavendish/content.css" title="Cavendish" media="all">
  <link rel="stylesheet" type="text/css" href="/css/cavendish/template.css" title="Cavendish" media="screen">
  <link rel="stylesheet" type="text/css" href="/css/cavendish/home.css" title="Cavendish" media="screen">

<?php include("dynmenu-head.php"); ?>


<!-- Dynamic Menu, http://codex.wordpress.org/Dynamic_Menu_Highlighting -->
$loc = getenv('SCRIPT_NAME');
if ( preg_match("/^\/links/", $loc) ) { $curr = 'links'; }
elseif ( preg_match("/^\/wiki/", $loc) ) { $curr = 'wiki'; }
elseif ( preg_match("/^\/blog/", $loc) ) { $curr = 'blog'; }
elseif ( preg_match("/^\/photos/", $loc) ) { $curr = 'photos'; }
elseif ( preg_match("/^\/about/", $loc) ) { $curr = 'about'; }
else { $curr = 'home'; }

<style type="text/css">
#header ul a#<?php echo $curr; ?> {
color: yellow;
#header ul a.out {
font-weight: normal;


<a name="top">
<div id="header">
 <h1><a href="/" title="Back to home page" accesskey="1">NO TEXT</a></h1>

  <li id="links"><a class="out" href="http://del.icio.us/bwiese" title="bwiese's del.icio.us links">Links</a></li>
  <!--  <li><a href="/norway/" title="brian's semester in Norway">Norway</a></li> -->
  <!--  <li><a href="/misc/" title="misc and old things">Misc</a></li> -->
  <li id="l_wiki"><a id="wiki" class="out" href="/wiki/" title="wiki notes howtos docs">Wiki</a></li>
  <li id="l_blog"><a id="blog" class="out" href="/blog/" title="brian's views and activities">Blog</a></li>
  <li id="l_photos"><a id="photos" href="/photos/" title="a slight obsession with photography">Photos</a></li>
  <li id="l_about"><a id="about" href="/about/" title="info on brian and this site">About</a></li>
  <li id="l_home"><a id="home" href="/" title="brian's homepage">Home</a></li>

 <form id="search" method="get" action="http://www.google.com/custom" title="search brianwiese.net">
  <label for="q" title="Search this site">Google this site: </label>
  <input type="hidden" name="domains" value="brianwiese.net">
  <input type="hidden" name="sitesearch" value="brianwiese.net">
  <input id="q" type="text" name="q" size="30" value="">
  <input id="submit" type="submit" value="Go">
  <input type="hidden" name="forid" value="1"></input>
</div> <!-- end header -->

Gallery mods

proper way

Discovered “html_wrap” directory after reading Upgrading Gallery 1

  • Edit files in brianwiese.net/photos/html_wrap to add the php includes.
    • cp gallery.header.default gallery.header
    • cp album.header.default album.header
  • Within the code, edit such as the following:
// $Id: album.header.default,v 1.19 2005/08/15 23:01:22 jenst Exp $

 /* changes by Brian Wiese */
 /* end changes by Brian Wiese */

if (!includeTemplate('album.header.tpl')) {

old wrong way

In brianwiese.net/photos/ Gallery v1 installation, I did not find a quick theme/template to edit 1), so I edited the following files:

  • util.php
    • contains the common_header() function called by nearly all gallery *.php pages
    • Need to get my headtext.php in here for CSS rules
  • albums.php, view_album.php, view_photo.php
    • the main view, need to include dynmenu-head.php after call to common_header() above 2), then also include header.php just after the <body> tag


function common_header() {

// Do some meta tags

// Import CSS Style_sheet
        echo getStyleSheetLink();

// Set the Gallery Icon
        echo "\n  <link rel=\"shortcut icon\" href=\"". makeGalleryUrl('images/favicon.ico') . "\">\n";

// added for common header menu by bkw Brian Wiese
        $includefile = '../inc/headtext.php';
        $rval = readfile($includefile);


after common_header

  <title><?php echo $gallery->app->galleryTitle ?></title>
        common_header() ;
        echo "<!-- Dynamic menu include, in albums.php -->";

after body

<body dir="<?php echo $gallery->direction ?>">

<!-- modification by bkw, brian wiese -->
<? include ("../inc/header.php"); ?>
<!-- end modification by bkw -->

Wordpress mods

  • Should work, edit the menus and header in my custom theme based off of random photo
    • Need to rename #header for CSS conflicts, seems to blow out main page graphic
  • Just using built in menu

Dokuwiki mods

  • May be able to do something with the templates, tpl_ files
  • Just created quick links for now


1) the jenkins and all gallery themes only seem to be images and .css files
2) could not place dynmenu-head.php code inside common_header()
howto/dynmenus.txt · Last modified: 2006/02/19 22:52 by
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki