Home

Login / Register

My Menu

Links

Identifying and Formatting Current URL from Within Custom Menu in Drupal.

The code snippet about creating a custom php code based link menu shown in tutorial at http://www.dashplanet.com/webmasters/drupal-cms/customizing-the-drupal-l... does well to let us know how to generate a custom php code based menu. This tutorial adds to it, it teaches us how to identify and then apply custom formatting to the current/active url.

Consider a scenario where you want to highlight the pages/url which is currently loaded. There can be many ways of doing this. More controlled and reliable way is described below. If you have read the tutorial at http://www.dashplanet.com/webmasters/drupal-cms/customizing-the-drupal-l... then observe the additions this tutorial makes.

"CurrentURL()" returns current url and I just added an if condition before printing out each of the links that if current url matches then add span around the link title so that we can apply some class to it.

Note 1: You will notice I have checked both url structures e.g. with and without 'www' prefix because in most of the cases you will not have handled to redirect your requests to either with 'www' or without so its good to check them both.

Note 2: You will also notice that instead of just adding a class to existing a or li tags I have wrapped the link title in span and applied a css class because otherwise it can be overridden by your existing css styles that you might have applied to all the li tags such as no background so spanning ensures that your formatting/styling works just as you want.

<?php
function CurrentURL() {
$pageURL = 'http';
if (
$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if (
$_SERVER["SERVER_PORT"] != "80") {
 
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
 
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return
$pageURL;
}

$url=CurrentURL();

global $user;

if ($user->uid) {
print
"<ul id='mylinkbar'>";

if($url=='http://www.dashplanet.com/' || $url=='http://dashplanet.com/'){
print
"<li><a href='http://www.dashplanet.com' ><span class='current_url'>Home</span></a></li>";
}
else{
print
"<li><a href=' http://www.dashplanet.com'>Home</a></li>";
}

if($url=='http://www.dashplanet.com/about' || $url=='http://dashplanet.com/about'){
print
"<li><a href='http://www.dashplanet.com/about' ><span class='current_url'>About</span></a></li>";
}
else{
print
"<li><a href=' http://www.dashplanet.com/about'>About</a></li>";
}

print "<li>"; print l($user->name,"user"); print "</li>";
print
"<li>"; print l(" Log Out " ,"logout"); print "</li>";
}

print "</ul>";
?>

And in your stylesheet you can add something like:

.current_url{
background: #111111;
color:#eeeeee;
}
No votes yet

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.