January 23, 2008

Quick N Dirty

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 = '/


Wordpress get image from post?
25 comments on page 1065

Photo gallery html5?
6 comments on page 1305

I use wpunlimited theme?
3 comments on page 1141

Slidebar image gallery html5?
4 comments on page 1053

Html5 photo gallery code?
6 comments on page 1305

Wpgetattachment?
25 comments on page 1065

How do you style polldaddy css?
2 comments on page 42

Disqus themes?
7 comments on page 1175

Css log in screend?
no comment on page 128

Is wordpress a framework for php?
2 comments on page 335

Wordpress featured images?
25 comments on page 1065

Wordpress featured images?
25 comments on page 1065

Disqus add image in comment?
7 comments on page 1175

Wpcache?
9 comments on page 1310

Funny wordpress plugins?
2 comments on page 1376

Css login screen?
no comment on page 128

Photoshop framework?
4 comments on page 296

Wpfun?
2 comments on page 1376

Wordpress fun plugins?
2 comments on page 1376

Get alll images of post wordpress?
25 comments on page 1065

Gallery html5?
6 comments on page 1305

Customisable short url?
4 comments on page 1190

Wordpress featuredimage?
25 comments on page 1065

Wp multiple sites?
one comment on page 1367

Wpupdatepost not working?
no comment on page 122

Html5 photo gallery code?
6 comments on page 1305

Html5 photo gallery code?
6 comments on page 1305

Flexx theme review?
4 comments on page 1026

Css login screen?
no comment on page 128

Html5 image gallery?
6 comments on page 1305

Wordpress get post image?
25 comments on page 1065

Show image in post?
25 comments on page 1065

Html 5 gallery?
6 comments on page 1305

Wpgetattachmentimagesrc?
25 comments on page 1065

Getimagefrompost resize?
25 comments on page 1065

Fun wordpress?
2 comments on page 1376

Wordpress workflow?
3 comments on page 1226

Easy peasy images featured image?
25 comments on page 1065

Google charts tinymce plugin?
7 comments on page 71

Html5 photo gallery code?
6 comments on page 1305

Simple css login screeen?
no comment on page 128

Wp flexx?
4 comments on page 1026
  every 1716s, 1s ago, in 0.04s.
Post a comment?

0s