Today I put the finishing touches to my customized archives widget. While I am tempted to submit it to the WordPress plugin repository I will hold off for a little bit. The reason: I’m not quite ready. That will usually be my excuse.
Enough of that on to the code!
The issue
I love using archives only because I love some aspects of history. The one problem I had with the way the core widget handles it is that it shows every single month with no set limit. I wanted to be able to set a limit without having to use code or a child theme to modify the widgetized areas.
The steps
In order to create a simple plugin one of the first things I had to do was look at the core default widgets. That file is found under
/wp-includes/default-widgets.php
and houses all the basic widgets that come standard with WordPress. Next, I created a blank folder with a blank PHP file with the name of my new plugin; used the standard header for a plugin and input all my information:
/*
Plugin Name: Lost Archives
Plugin URI:
Description: Custom Archive Widget with a few extra options.
Version: 0.1
Author: Jose Castaneda
Author URI: http://josemcastaneda.com
License: public domain
*/
The next thing was to get the basic logic behind what I wanted my newly created widget to do. What information I wanted to gather from a user and what to pass so that it will display properly on the front end. The basic one being the title of the widget. Next I wanted the ability to pass a number so that it sets the limit to how many things to show. Finally I wanted the ability to to chose what type of archive to display; if I wanted a weekly, daily, or monthly type of archive.
The code
In order to gather the information I first needed a form. Thankfully WordPress makes things pretty easy with the widget class. By creating a new class that extends the WP_Widget class I can override the
function form(){}
and use it to create the form the user sees on the back-end. This was a little tricky in that I had to run a check for what is currently selected and what isn’t; and, again, WordPress has a function for this:
selected( $selected, $current, $echo)
In order to display the different options I had to use a foreach()
loop in order to display the various choices. The code looked a little like:
foreach( $array as $key => $value ){
echo $key;
}
Now I know I could have cleaned things up a little bit but that is for another time and another post.
The end result
And the final product is:
<?php
/*
Plugin Name: Lost Archives
Plugin URI:
Description: Custom Archive Widget with a few extra options.
Version: 0.1
Author: Jose Castaneda
Author URI: https://blog.josemcastaneda.com
License: public domain
*/
add_action( 'widgets_init', 'lost_archive_widget' );
function lost_archive_widget(){
register_widget( 'lost_archives' );
}
class lost_archives extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'archives_extended',
'description' => esc_html__( 'Customized Archives wigdet.', 'text-domain' )
);
parent::__construct( 'solea_archives_widget', esc_html__( 'Archives', 'text-domain' ), $widget_ops );
}
function widget( $args, $instance ){
extract( $args );
$limit = ( empty( $instance['limit'] ) ) ? '12' : $instance['limit'];
$type = ( empty( $instance['type'] ) ) ? 'monthly' : $instance['type'];
$title = apply_filters('widget_title', empty($instance['title']) ? __( 'Archives' ) : $instance['title'], $instance, $this->id_base);
$content = wp_get_archives( array(
'type' => $type,
'limit' => $limit,
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 0,
'order' => 'DESC'
) );
$output = '%1$s %2$s %3$s %4$s <ul>%5$s</ul> %6$s';
printf($output, $before_widget, $before_title, $title, $after_title, $content, $after_widget );
}
function update( $new_instance, $old_instance ){
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'type' => '', 'limit' => '') );
$instance['title'] = strip_tags($new_instance['title']);
$instance['limit'] = $new_instance['limit'];
$instance['type'] = $new_instance['type'];
return $instance;
}
function form( $instance ){
$instance = wp_parse_args( (array)$instance, array( 'title', 'limit', 'type' ) );
$title = strip_tags($instance['title']);
$limit = $instance['limit'];
$type = $instance['type'];
$types = array(
esc_html__( 'Post', 'text-domain' ) => 'postbypost',
esc_html__( 'Daily', 'text-domain' ) => 'daily',
esc_html__( 'Weekly', 'text-domain' ) => 'weekly',
esc_html__( 'Monthly', 'text-domain' ) => 'monthly'
); ?>
<label for="<?php echo $this->get_field_id('title'); ?>">< ?php _e('Title:'); ?></label>
<input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="< ?php echo esc_attr($title); ?>" />
<label for="<?php echo $this->get_field_id('limit'); ?>">Limit:</label>
<input id="<?php echo $this->get_field_id('limit'); ?>" class="widefat" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="< ?php echo esc_attr($limit); ?>" />
<label for="<?php echo $this->get_field_id('type'); ?>">Type:</label>
<select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>">
<?php foreach( $types as $k => $t ){
echo '<option selected="selected" value="' . $t . '">'. $k . '</option>';
} ?>
</select>
<?php
}
}
Keeping in mind this works with WordPress 3.5 and above and I haven’t tested any other versions.