Removing Menu Links to the Current Page

2007/11/30 | By | 8 Replies More

One of the fundamental usability guidelines is: never have a link that points to the current page – this confuses the user. (See Jakob Neilsen’s The Ten Most Violated Homepage Design Guidelines, guideline no. 10.) The WordPress blogs and websites that I have seen (including this blog!) violate this guideline in several ways. It would be nice if a future version of WordPress would give these functions an option to not link to the current page or category. In the meantime, I have written a plugin that strips the link to the current page. It is used on the web site for my church which is under development.

Here is the code for the plugin, I named the file rm_cur_menu_link.php. It is installed in the wp-content/plugins directory, then it may be activated in the plugin administration screen. This plugin will rermove self links in menus that are generated by the wp_list_pages() and wp_list_categories() functions.

<?php
/*
Plugin Name: Remove Current Menu Link
Plugin URI: Does not exist yet
Description: Removes link to the current item from a menu generated by wp_list_pages and wp_list_categories. This makes a WordPress website or blog comply with the usability guidelines that a web page should not have a link to itself.
Version: 1.0
Author: Bill Stoddard
Author URI: http://clockinfo.com/
*/

/* Copyright 2007 Bill Stoddard (email : [email protected])

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/**************************************

Hooks and configuration

***************************************/
add_filter(‘wp_list_pages’,’list_pages_filter’);
add_action(‘wp_list_categories’, ‘list_categories_filter’);

function list_pages_filter($content) {
/* for some reason it would not work with [:blank:]* after the m in current_page_item */
return eregi_replace(‘(current_page_item “><a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)’,’current_page_item”>\\3′, $content);
}

function list_categories_filter($content) {
return eregi_replace(‘(current-cat[:blank:]*”><a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)’,’”>\\3′, $content);
}

?>

I still consider this plugin to be experimental, although it works fine in my application (WordPress version 2.3.1), so I have not released it as a downloadable plugin yet. You can see it in action on the Flora First Christian Church website.

I thank silisoftware.com and xce.de for their helpful comments on the eregi_replace manual page.

Category: WordPress

Last updated: 2007/12/03

Comments (8)

Trackback URL | Comments RSS Feed

  1. Bill says:

    To make it work with WordPress version 3.2.1, I made the following change:

    Change the line:

    return eregi_replace(‘(current_page_item “><a [^]*>([^<]*))’,’current_page_item”>\\3′, $content);

    to:

    return eregi_replace(‘(current_page_item”><a [^]*>([^<]*))’,’current_page_item”>\\3′, $content);

    It is just the removal of one space after current_page_item

  2. Dom says:

    I’ve created a ticket in WordPress, with a patch that offers a new parameter to avoid that problem: http://core.trac.wordpress.org/ticket/15811

  3. Dom says:

    I’ve posted a cleaned up version of the code of the plugin at https://github.com/lewebmobile/Self-link-remover-Wordpress-plugin since the code that appears here has been mangled by WordPress.

    Very useful, plugin, thanks; it certainly sounds like something that should be added as an option to the relevant functions in WordPress core.

  4. sandrar says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  5. Frank P says:

    Yeah, this should really be sorted out in WordPress – analytics shows that a LOT of visitors click the link to the page their on.

    Love to see a fully functional plugin for this.

  6. Dusty says:

    Not working as of WordPress 2.7. The error received is: Parse error: syntax error, unexpected T_STRING in (*server path*) on line 38.

  7. Bob says:

    Links to pages that the user is actually reading is annoying, if not entirely daft (it’s a pet hate). For those that are serious about accessibility, this is a must-have for WP. (pls) Consider putting it out as a plug-in as I’m sure it will be useful, if not improved over time. Good work.

Leave a Reply