<?php
/*
Plugin Name: Monthly archives drop-down
Plugin URI: http://www.qwghlm.co.uk/?p=740
Description: Produces a proper, non-Javascript dropdown list of month-by-month archives
Version: 1.00
Author: Chris Applegate
Author URI: http://www.qwghlm.co.uk/


Usage: Just call make_month_dropdown(); where you want to make your dropdown.

Optional parameters include:

  - $button_label : The inscription on the "Go" button
  - $button_class : Optional CSS class to assign to the button

*/


function make_month_dropdown($button_label='Go'$button_class='') {
    global 
$wpdb$tableposts$month;
    
$now current_time('mysql');

    
// Get the entries grouped by month and year
    
$archiveEntries $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`
                                    FROM $tableposts WHERE post_date < '$now' AND post_status = 'publish'
                                    GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC"
);

    
// Start form
    
echo '<form action="'.get_settings('home').'/" method="get">';
    echo 
'<div><select name="m" class="small" onchange="this.form.submit();">';

    
// Produce each line of the dropdown
    
foreach($archiveEntries as $archiveEntry) {

        
// Add leading zero
        
$thisMonth zeroise($archiveEntry->month,2);
        
// value to be passed into query string
        
$thisM $archiveEntry->year.$thisMonth;

        echo 
"<option value=\"$thisM\"";

        
// Preselect currently-viewed month
        
if ($thisM == $_GET['m']) echo ' selected="selected" ';

        echo 
'>';
        echo 
$month[$thisMonth].' '.$archiveEntry->year;
        echo 
'</option>';
    }

    echo 
'</select>&nbsp;<input type="submit"';
    if (!empty(
$button_class)) echo ' class="'.$button_class.'"';
    echo 
' value="'.$button_label.'" />';
    echo 
'</div></form>';
}