Quick n’ Dirty Bookmark Navigation

Posted: 28th Jan 2008, in: Snippets, plugins - Older Post - Newer Post

Quick N Dirty

This is the fifth of my Quick N Dirty plugin posts and it is a little more involved than the previous four. It isn’t any more difficult, in fact it is a very simple plugin, but it does require adding a tag to your theme, and adding some categories to your bookmarks. None of this is difficult though so I don’t foresee many problems.

In this post I am going to show you how you can use the blogroll functionality, and a small plugin, to create a fully controllable menu for your website. Now, by fully controllable I mean that you can move items about within the menu, set the links in any way you please and choose what links you display to normal visitors and what links you display to users that are logged in.

So how do you do this? I will go through it in steps; but before I start I should mention that this may not work on versions of WordPress below 2.3.

Step 1

The first thing you need to do is to log into your blog, choose blogroll, and create two new categories. The first is a list of links that you want every visitor to see, and the second is a list of links you only want users that are logged in to your website to see. Now note down the ID of each category. (In my example 15 is for all visitors, and 16 is for logged in users)

You should now create at least one link in each. Note that in the advanced panel there is a rating option. We will use this later on.

Step 2

Before we worry about the plugin you need to insert the list into your template. To do this you need to use the template tag: wp_list_bookmarks. The example below will create a pure list, without any titles, and will order it according to the rating field that I mentioned earlier.

<ul>
<?php wp_list_bookmarks(‘category=15&categorize=0&title_li=0&orderby=rating’); ?>
</ul>

Notice that I have given it category 15, the category ID that is available to everyone. You need to replace this with your category ID.

Also note that the rating field only allows you to rate from 0 to 9 so if you will have more lists than this you might want to use the notes column instead.

The plugin is going to insert the links that are assigned to category 16 automatically, so when you are rating your links, if you want a member only link to appear at a given point in the list you should leave that blank in the all user list; for example, if you want the first five links to be from the all-user list, rate them 1-5 in the order you want them. If you want the sixth link to be from the logged-in-only list then find that link and rate it 6, you can then rate your next all-user link 7 to continue the list.

None of this will matter though until the plugin is added.

Step 3

The plugin uses a filter: get_bookmarks. This is called whenever a request for a list of bookmarks is put in. If the request is only for the all-user list then the plugin checks to see if the user is logged in, and if the user is logged in automatically adds the logged-in-only links to list.

Here is the plugin:

<?php
/*
Plugin Name: Quick n’ Dirty Bookmark Navigation
Plugin URI: http://www.wp-fun.co.uk/2008/01/28/quick-n-dirty-bookmark-navigation/
Description: Adds a specified bookmark category to a list if the user is logged in.
Author: Andrew Rickmann
Version: 1
Author URI: http://www.wp-fun.co.uk
*/
 
//This is the line that adds your filter into the list. 
// ‘tget_bookmarks’ is the name of the filter
// ‘qnd_bookmark_navigation’ is the name of the function, below
// 1 is the priority, 1 being highest, and 10 lowers
// 2 is the number of arguments the function expects to recieve.
add_filter( ‘get_bookmarks’ , ‘qnd_bookmark_navigation’ , 1 , 2 );
 
function qnd_bookmark_navigation( $bookmarks , $options ){
 
	//first we set our category id numbers. 	
	$normal_nav_id = ‘15′; //change this one to match your all-user list
	$member_nav_id = ‘16′; //change this one to match your logged-in-only list.
 
	//to make sure that we don’t trigger an infinite loop
	//if we are in the admin screen, or asking for any category(ies) except 
	//the all-user category we pass back the bookmarks and escape.
        //The bookmarks must always be passed back or else no list will appear at all.
	if ( is_admin() || $options[‘category’] !== $normal_nav_id ) { return $bookmarks; }
 
	//if the user is logged in and the requested category is all-user:
	if ( is_user_logged_in() && $options[‘category’] === $normal_nav_id ) {
 
		//change the list options so that we are requested the additional category
		$options[‘category’] = $normal_nav_id . ‘,’ . $member_nav_id;
 
		//regenerate the list with both the categories included
		$bookmarks = get_bookmarks($options);
 
		//send them on their way.
		return $bookmarks;
	}
}
 
?>

I hope it should be fairly clear what is going on in this plugin, after all it is very simple, but feel free to ask any questions you’re not sure about.

Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.

 

Comments

Leave a Reply

I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.

Please note. I am currently using an experimental antispam technique on this blog. If you run into problems please let me know using the Get in Touch link at the top of the page. Thanks, Andy.

Subscribe without commenting

Feed Icon - Get fed with RSS