Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Formatting End-of-page Site Links

Generally, or often, or traditionally — I'm uncertain which word to use here — there are some links below the main content of a web page. Here, you'll find the Terms of Service, About Us, or other links the site owner makes available for those who are particularly interested.

Those very same links are at the bottom of every page, even when you're on a page one of the links refer to.

There is nothing technically wrong with presenting a link to the Terms of Service, for example, while on the Terms of Service page. But it can be perceived as the site owner not paying attention or caring about details.

In other words, it can be seen as inelegant.

There is a way to automatically omit an end-of-page link when the browser is at the web page that would otherwise be linked to. It's accomplished with PHP.

Because it is a new website, the functionality was first implemented at Spam-free Form. It is the demonstration website for this article.

Currently, the Spam-free Form site has three end-of-page links, but only two when one of the linked-to pages is in the browser window.

Because "Home" is one of the three links, you see only two linked-to pages at the bottom of the site index page. If you're curious, the contact page has all three links.

To accomplish it, the new Location-aware Menu Links Publisher is used. It's a PHP script that can be pulled into your web pages from an external file.

How It Works

You tell Location-aware Menu Links Publisher the URL and link text for each link to be published. And let it know what character and/or space to separate the links with.

When a page loads, the script creates the links using each link that's not the same as the current page. The links are stored in a PHP variable with the $LocationAwareMenuLinks name.

$LocationAwareMenuLinks can be used anywhere on the web page to publish the links.

The Source Code

Here is the source code of Location-aware Menu Links Publisher as currently used at SpamFreeForm.com. Installation instructions follow.

<?php
/*
Location-aware Menu Links Publisher
Version 1.0
October 5, 2018
Will Bontrager Software, LLC
https://www.willmaster.com
*/

/* Three places to customize. */
// Place 1 --
// Specify "Yes" or "No" (not case-sensitive) for 
//    whether or not menu item text shall be prevented 
//    from wrapping. ("Yes"=Keep together. "No"=Wrap OK)

$TitleKeepTogether = "yes";

// Place 2 --
// Specify the separator character or characters 
//    (they get inserted between the menu items).

$Separator = " <span style='color:#999;'>&#9679;</span> ";

// Place 3 --
// Between the lines with the capitalized word LINKS 
//    type the URL and the link text of every menu 
//    link, one URL/title set per line. The URLs need 
//    to be be absolute http:// or https:// URLs or 
//    URLs relative to document root.

$links = <<<LINKS
/index.php Home
https://spamfreeform.com/terms.php Terms of Service
/privacy-policy.php Privacy Policy
LINKS;

/* End of customization area */
if( strtoupper($TitleKeepTogether[0]) == 'Y' )
{
    $titlestart = '<span style="white-space:nowrap;">';
    $titlestop = '</span>';
}
else
{
    $titlestart = '';
    $titlestop = '';
}
$LinksList = array();
foreach( preg_split('/[\r\n]+/',trim($links)) as $line )
{
    $line = trim($line);
    if( strpos($line,' ')===false ) { continue; }
    list($url,$title) = preg_split('/ +/',$line,2);
    $urlkey = preg_replace('!https?://[^/]+!','',$url);
    $LinksList[$urlkey] = "$url\t$titlestart$title$titlestop";
}
$MenuArray = array();
foreach( $LinksList as $urlkey => $urltitle )
{
    if( ! preg_match('/'.preg_quote($urlkey,'/').'/',$_SERVER['PHP_SELF']) )
    {
        list($url,$title) = explode("\t",$urltitle);
        $MenuArray[] = "<a href='$url'>$title</a>";
    }
}
$LocationAwareMenuLinks = implode($Separator,$MenuArray);
?>

Installation Instructions

The installation instructions have 3 steps.

  1. Customizing the script.
  2. Putting the script on the server.
  3. Publishing the menu.

A. Customizing Location-aware Menu Links Publisher:

There are three places to customize in the Location-aware Menu Links Publisher source code.

  1. Variable $TitleKeepTogether needs to know whether or not to prevent menu item text (the linked text) from wrapping. If yes, give it any value that begins with the letter "Y" (any case) such as "yes" or "Yes". Otherwise, (to let menu item text wrap) a value that does not begin with the letter "Y", like "no".

    If the value begins with the letter "Y", menu item text won't have a line break within them but any complete menu item text can wrap to separate lines. In other words, with a "Y" word this can happen:

    Terms of Service | About Us

    Or this can happen:

    Terms of Service |
    About Us

    But not this:

    Terms of
    Service |
    About 
    Us
  2. Variable $Separator needs to know what character or characters to insert between each menu item. HTML code may be used.

  3. Variable $links needs a list of URLs and their menu item text — each URL/menu-item set on a separate line.

    Put them between the lines that contain the word LINKS.

    The URL can be an absolute URL that begins with http:// or https:// URL or it can be a URL that relative to the document root and begins with a "/" character. Example:

    $links = <<<LINKS
    /index.php Home Page
    https://example.com/about.php About Us
    /terms.php Terms of Service
    LINKS;
    

B. Where to put Location-aware Menu Links Publisher:

Save the Location-aware Menu Links Publisher file as menulinks.php (or other *.php name that works for you) and upload it to your server. Make a note of its location.

C. Publishing Menu Links:

Two lines of PHP code (between <?php and ?>) pull in the script and publish the menu. Put these on every page at the spot where you want the menu published.

<?php 
include("{$_SERVER['DOCUMENT_ROOT']}/location/menulinks.php"); 
echo($LocationAwareMenuLinks);
?>

Replace /location/menulinks.php with the location of the Location-aware Menu Links Publisher script file on your server.

It is now installed.

The Easy Part

Once the script is customized and uploaded to the server, step C is the only one that needs to be repeated on each page. If the menu is published in a template, or in an external file pulled in with PHP or JavaScript, then the template or external file is the only place step C needs to be done.

If the menu changes, change only the customization area of the script and you're good to go.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC