Open-source, free PHP breadcrumb link script

8 Comments

Topics: Hacks

Last night, I was working on the new Simpli website. The designers who did the website had a Javascript that created breadcrumbs at the top of each page (you know, those links that say Home > Section Name > Page Name). I thought it was really silly to do that in Javascript; plus, the code was hard to modify and had a bug in it that was difficult to fix since the variables were named (I am serious) a, b, foo, and bar. Ugh!

I searched and found this free PHP breadcrumb script, but it didn’t do some things that I wanted — namely, if the visitor is on the index page of a section, it shouldn’t link that section name (since the link would be to the same page the visitor is already on.) Also, I wanted the final breadcrumb to be the page name if the page wasn’t an index page (for instance: Simpli Hosting > About Us > Our Datacenter, instead of Simpli Hosting > About Us for the Our Datacenter page.) That script didn’t offer that.

Also, even though I had “convert underlines in page filenames to spaces” and “uppercase the first letter of every word” enabled, they didn’t actually work. So I fixed that too.

Since the original author released the PHP breadcrumb script free of charge, I will release my modifications free of charge, and document my modifications below.

Here is the script: breadcrumbs.phps

You use it simply by including it in the page, and it will output the breadcrumb based on the directory structure of your site. For instance, a directory structure of http://www.simpli.biz/FAQ/Colocation_FAQ.php will generate:

Simpli Hosting » FAQ » Colocation FAQ

Change the $topLevelName and $separator variables to change the text in the first part of the breadcrumb (in this case, “Simpli Hosting”) and the separator between each breadcrumb (in this case, ».)

The key here is that if you’re on the page http://www.simpli.biz/FAQ/index.php, it won’t print “index” and it won’t link “FAQ”. However, if you’re on http://www.simpli.biz/FAQ/Colocation_FAQ.php, it will link FAQ to http://www.simpli.biz/FAQ/index.php.

This script does assume index.php is your directory index in every directory, and it does convert underscores in pages to spaces and uppercase the first letter of every word in the breadcrumb. If you don’t want it to do this, you can comment out the lines in the code that do that. See if you can figure out which lines do that.

This script is free to download, free to use (public domain), but there is no support. If it works for you, great! If not, hack it till it does. ;)

One catch: My modifications use $_SERVER. If you use $HTTP_SERVER_VARS, you will need to global $server and change $_SERVER to $server in the MPBCDirIndex function.

Email This Post Email This Post   | Print This Post Print This Post


Previous post in this category:

Posted on Saturday, June 23rd, 2007

  • TechnoLust

    Have I ever told you that I find girls who can code really hott? :-)

  • SlashChick

    You know, normally I would delete those sorts of comments, but since you are a friend I’ll just say “Thank you.” :)

  • http://www.dogster.com/ Barce

    Ya, don’t use globals! A good developer always knows the origin of every variables’ data.

  • http://www.laspositascollege.edu Elizabeth

    Hi Erica,

    Thanks for the breadcrumb code. Is there a way to use the page title instead of the filename as the last crumb?

    Thanks!

    Elizabeth

  • http://www.jaydaniells.com Jay

    Hi Erica,

    Great script! Being a designer who’s only just starting to look at how to write ‘hello world’ type PHP scripts I would have had no idea how to go about building this thing.

    … And of the variety of PHP breadcrumb scripts I’ve previously found on the net and tried out your above modification has been the most successful. It’s features were exactly what I was looking for.

    Thanks again!

  • http://www.emiliosgreekrestaurant.co.uk Nick C

    hi

    i have struggled to get the breadcrumbs to show sub directories is it something i am doing wrong or something the breadcrumb does to make it better SEO

    my layout is
    home
    |–menu
    |–xmas
    |–main
    |–new_year

    but the breadcrumb never creates the same structure.. all files were index.php in each directory

    can you explain why.. thanks

  • http://burstsms.com web sms devoted

    I hate cleaning up someone else’s crumbs at the end of the day. I don’t say I am an expert but some people do stupid things.

  • Becca Powell

    2 things I love here: 1) after hours of searching and trying other scripts I FINALLY found a breadcrumbs script that works and is easy to understand, and 2) it was written by a female. Win-win. Thank you!!