January 28, 2008

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.

[php]

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.



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1741s, 1s ago, in 0.03s.
Post a comment?

0s