<?php
/*
Plugin Name: List Pages - First and Last li class
Plugin URI: http://wordpress.org/#
Description: Wordpress Plugin that adds a 'first_item' and 'last_item' class to the first and last list items generated from the wp_list_pages function.
Author: Eliot Kristan
Version: 1.0
Author URI: http://www.eliotk.net
*/ 

//
function addFirstLastClass($pageList) {
    
    
// pattern to focus on just lis
    
$allLisPattern '/<li class="page_item(.*)<\/li>/s';
    
preg_match($allLisPattern,$pageList,$allLis);
    
$liClassPattern =  "/<li[^>]+class=\"([^\"]+)/i";
    
    
// first let's break out each li    
    
$liArray explode("\n",$allLis[0]);
    
    
// count to get last li
    
$liArrayCount count($liArray);
    
    
$lastLiPosition $liArrayCount-1;
    
    
// get the class name(s) of first class and last class
    
preg_match($liClassPattern,$liArray[0],$firstMatch);
    
preg_match($liClassPattern,$liArray[$lastLiPosition],$lastMatch);
    
    
// add the new class names and replace the complete first and last lis
    
$newFirstLi str_replace($firstMatch[1],$firstMatch[1]. " first_item",$liArray[0]);
    
$newLastLi str_replace($lastMatch[1],$lastMatch[1]. " last_item",$liArray[$lastLiPosition]);
    
// replace first and last of the li array with new lis
    
    // rebuild newPageList
        // set first li
        
$newPageList .= $newFirstLi.'';
        
$i=1;
        while(
$i<$lastLiPosition)    {
            
$newPageList .= $liArray[$i];
            
$i++;
        }
        
// set last li
        
$newPageList .= $newLastLi;
    
        
// lastly, replace old list with new list
        
$pageList str_replace($allLis[0],$newPageList,$pageList);
    return 
$pageList;
}

add_filter('wp_list_pages''addFirstLastClass');

?>