Archive for the ‘Snippets’ Category

This is the sixth of my Quick N Dirty plugin posts and I am doing something different today; I am going to explain a plugin created by someone else.
The plugin I want to focus on today is the bm_custom_login plugin. This plugin allows you to replace your WordPress login screen with something a little more branded, and is a brilliant demonstration of just how easy it can be to produce a plugin.
Just like all the plugins in this series this uses only one hook, and actually has even less code than I have used so far.
The hook is uses is: login_head
add_action('login_head', 'bm_custom_login');
This action fires in the HTML head of the login page allowing you to add any CSS, or Javacript files you want to. The plugin outputs only one line of HTML, but the effects are significant. Here is the function it calls:
function bm_custom_login() {
echo '<link rel="stylesheet" type="text/css" href="' . get_settings('siteurl') . '/wp-content/plugins/bm-custom-login/bm-custom-login.css" />';
}
It links to a CSS file in the plugin folder that points the login screen towards the custom images in the plugin folder.
I won’t reproduce any more of the plugin here, I encourage you to download a copy from the Binary Moon site and take a look at it. It has everything you need, the plugin, the CSS file and a template image to help you produce your own.
My version is on the left.
Tomorrow I am going to start looking at how plugins link in to the admin pages themselves. I will be demonstrating a plugin to create an admin page and display information on it.

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.

This is the fourth of my Quick N Dirty plugin posts. In the first I used the the_content filter to repeat the links used in a post, at the end of that post. In this post I am going to the_content again, but this time I am specifically using it in place of another filter that could get you quite confused.
It is a pretty common thing to do these days, add a footer to each post in your feed. There are a few plugins that not only let you do it but give you a handy admin page to alter the content as well. Nothing beats knowing how to do it yourself though.
If you are going to do it yourself, there is one area that provides some confusion. There is a filter that sounds like it should be used, but simply won’t work for you. That filter is the_content_rss.
The problem is that the_content_rss is used in rss, and rdf feeds, but importantly not in the rss2 feeds which is what we all tend to use these days. So if you want to change the content of your feeds, for the moment at least, you need to use the_content and check to see whether a feed is being requested. Luckily this is very easy.
Here’s the plugin:
<?php
/*
Plugin Name: Quick n' Dirty Feed Footers
Plugin URI: http://www.wp-fun.co.uk/2008/01/27/quick-n-dirty-feed-footers/
Description: Adds content to the bottom of each rss2 post
Author: Andrew Rickmann
Version: 1
Author URI: http://www.wp-fun.co.uk
*/
//This is the line that adds your filter into the list.
// 'the_content' is the name of the filter
// 'qnd_feed_content' is the name of the function, below
add_filter( 'the_content' , 'qnd_feed_content' );
function qnd_feed_content( $content ){
//make sure that a feed is being requested
if ( is_feed() ) {
//create a string containing the html we want to add to the bottom
$additional_content = '<div style="border:1px solid #ccc; padding:5px; background-color:#DBD2DC">';
$additional_content .= '<h2 style="margin:0 0 5px 0; font-size:95%; color:#AC26A7">Interesting Message</h2>';
$additional_content .= '<p>The content of the interesting message</p>';
$additional_content .= '</div>';
//return the content, and the additional content to WordPress
return $content . $additional_content;
} else {
//if we are not within a feed just send back the original content
return $content;
}
}
?>
I have used a basic WordPress tag - is_feed() - to check if a feed is being accessed. Once known the content can be modified. Very important, as with previous plugins is to return the content without changes if it is not a feed. As this is a filter, if no content is returned then no content will be displayed on screen.
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.

This is the third of my Quick N Dirty plugin posts. In the second post I showed how to access the post object and alter the post content before saving it back to the database. This plugin will also access to the post object, but this time I will use it to exclude posts from being displayed on the home page.
The hook I am going to use for this plugin is the_posts filter. This filter is called when a list of posts is pulled from the WordPress database ready to be displayed on screen.
The filter passes an array of post objects to the function which gives us the opportunity to carry out mass edits, add posts, or, as in this case, remove posts in certain circumstances.
What I want to do is to make sure that posts in my General category do not appear on the home page. I want them as part of my feed, as an added sweetener for subscribing, and I want them as part of my single post and my archives.
Here’s the plugin, I will explain more at the bottom:
<?php
/*
Plugin Name: Quick N Dirty Post Exclusion
Plugin URI: [insert the plugin uri here]
Description: Removes posts from the home page
Author: Andrew Rickmann
Version: 1
Author URI: http://www.wp-fun.co.uk
*/
//call the filter
//the_posts is called when a list of posts is retrieved from the database
//qnd_post_exclusion is the function
//1 is the priority, 1 runs first, 10 last
add_filter('the_posts', 'qnd_post_exclusion' , 1 );
function qnd_post_exclusion( $posts ){
//only apply this if we are looking at the home page
if ( !is_home() ) { return $posts; }
//create an array to hold the posts we want to show
$new_posts = array();
//loop through all the post objects
foreach( $posts as $post ){
//for each object get an array of applicable categories
$cats = get_the_category( $post->ID );
//create a variable that determins if this post should be included
//the default is true, i.e. include the post
$include = true;
//loop through all the categories applicable to the post
foreach( $cats as $cat) {
//if the post is assigned to our undesirable category then do not include it
if ( $cat->name == 'General' ) { $include = false; }
}
//if we want to include it then add the post object to the new posts array
if ( $include === true ) { $new_posts[] = $post; }
}
//send the new post array back to be used by WordPress
return $new_posts;
}
?>
By now the add_filter function should be fairly familiar so I won’t dwell on it. It shows the the hook, the function to be used, and specifies that it should run with the highest priority. I have chosen high priority so that any plugins that follow on that might modify the content of the post are saved the trouble of modifying a post that will later be removed. Aside from that the priority isn’t too relevant here.
The usual template functions is_home, is_feed, is_archive, etc will work at this point and so I have used is_home to restrict the plugin to posts displayed on the home page. If we are not dealing with the home page then the function will simply return the original array of posts. It is essential that it does return the array. Not returning the array is the same as filtering out every post, leaving no posts at all.
When the function is called it is passed an array of post objects. We use the post ID in each case to get an array of the categories the post is assigned to using the function below:
get_the_category( $post->ID );
We then iterate through this array and if our chosen category is not in the array for each post we add the post to a new array . When we are done it is this array that is passed back to WordPress to continue its journey toward screen.
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.