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.



Netbeans wordpress?
6 comments on page 973

Html5 gallery?
6 comments on page 1305

Html5 gallery?
6 comments on page 1305

Html5 photo gallery code?
6 comments on page 1305

Wordpress plugin development?
2 comments on page 1373

Wordpress get attachment title?
26 comments on page 1065

Html 5 photo gallery?
6 comments on page 1305

Wp get all images in post?
26 comments on page 1065

Wpupdatepost wordpress?
no comment on page 122

Html5 photo album?
6 comments on page 1305

Wordpress fun?
2 comments on page 1376

Charcoal theme?
2 comments on page 959

Get url of featured image wp?
26 comments on page 1065

Addsettingsfield css id?
3 comments on page 793

Get featured image id wordpress?
26 comments on page 1065

Theposts filter?
11 comments on page 124

Fun wp theme?
2 comments on page 1376

Photo album in html5?
6 comments on page 1305

Addsettingsfield args array?
3 comments on page 793

Url shortener?
4 comments on page 1190

Wordpress fun plugin?
2 comments on page 1376

Intensedebate blocking css?
7 comments on page 1175

Charcoal theme?
2 comments on page 959

Wordpress html picture code?
21 comments on page 1073

Wordpress trim title?
no comment on page 1371

Wp categories vs tags?
12 comments on page 7

Wordpress image attachment metadata?
26 comments on page 1065

Clear wp cache?
9 comments on page 1310

Funny wordpress template?
2 comments on page 1376

Anchor tag with timthumbphp?
26 comments on page 1065

Disqus formatting?
7 comments on page 1175

Wordpress function attachment?
26 comments on page 1065

Post attachment in category?
21 comments on page 1073

Wordpress theimage?
26 comments on page 1065

Wordpress fun plugins?
2 comments on page 1376

Wordpress chat?
no comment on page 1308

Wordpress post title spacing?
no comment on page 1371

Html image gallery code?
6 comments on page 1305

Polldadylogo?
2 comments on page 42

Polldadylogo?
2 comments on page 42

Wordpress fun plugins?
2 comments on page 1376

Tableprefix wp?
2 comments on page 1374

Html5photo gallery?
6 comments on page 1305
  every 2094s, 1s ago, in 0.03s.
Post a comment?

0s