
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 = '/
page 27
page 1321