Quick N’ Dirty LinksFiled Under: Snippets, plugins

As this is the first of the Quick N Dirty plugin posts I’m going to explain the basic principles of WordPress plugins before getting to the plugin itself.
So what is a plugin?
At its most basic a WordPress plugin is just a PHP function that is called by WordPress during a specified process. As the plugin writer, you tell WordPress when you want your function to be called by adding your function to a list attached to one of a series of hooks. When WordPress gets to the point in the process that the hook applies to it will run all the functions in that list.
There are two types of hook: actions and filters.
Actions call the functions in the list without passing any data and without expecting any back. Often these are used to add content to the page in some way such as adding a copyright notice in the footer.
Filters are more involved. Filters pass data to the function and require that data, or an amended version, to be sent back. Filters are usually part of a data handling process such as outputing the content of a post to the screen.
For the first quick n’ dirty plugin I am going to use a filter: the_content.
This filter runs as part of the, the_content, template tag. When the tag is used in the template WordPress retrieves the content of the post, passes it through the filters that have hooked into the process, and then outputs it to screen. This means we can add anything we want, or amend anything we want, to the content of each post without altering the information in the database.
The fully commented plugin is included below. The plugin takes the content, retrieves all the links, checks ( using get_bloginfo ) it to see if the link points to my blog, or not, and then adds the links to the end of the post.
<?php /* Plugin Name: Quick N Dirty End of Post Links Plugin URI: http://www.wp-fun.co.uk/2008/01/23/quick-n-dirty-links/ Description: Captures all the links within a post and lists them all at the end of the content. 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_end_of_post_links' is the name of the function, below // 1 is the priority, 1 is called first, 10 last. add_filter('the_content', 'qnd_end_of_post_links' , 1 ); //WordPress will call this function //$content is the full content of the post function qnd_end_of_post_links( $content ){ //a very basic regular expression to get the links $regexlink = '/<a href="[^<]*<\/a>/i'; //apply the regular exporess, the links that match are saved in $matches preg_match_all( $regexlink , $content , $matches ); //create a string to contain the internal and external links $internal = ''; $external = ''; //loop through all the matching links foreach( $matches[0] as $match ) { //check if the link is pointing at our blog url, if so add to the internal string if ( strpos( $match , get_bloginfo('wpurl') ) !== false ) { $internal .= ' - ' . $match; } else { $external .= ' - ' . $match; } } //only add the header to each list if there are links in the list if ( strlen($internal) != 0 ) { $internal = '<h3>Links to my blog</h3>' . $internal; } if ( strlen($external) != 0 ) { $external = '<h3>Links to other blogs</h3>' . $external; } // !!important, filters must return the value or the post will be empty return $content . $internal . $external; } ?>
It is absolutely essential that the function you create has a unique name. If it is the same as any other function in WordPress then it will trigger an error. Plugin authors will always wrap their functions in classes to avoid this; however, for very simple plugins on your own blog, this isn’t really necessary.
This plugin is running on this blog right now. The next two headers and lists are the result of the plugin.
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.
- Permalink
- Andrew Rickmann
- 23 Jan 2008 6:49 PM
- Comments (0)