Skip to content

Instantly share code, notes, and snippets.

@Mamaduka
Created April 7, 2011 20:24
Show Gist options
  • Save Mamaduka/908634 to your computer and use it in GitHub Desktop.
Save Mamaduka/908634 to your computer and use it in GitHub Desktop.
I just recreated Akismet widget using WP_Widget class
<?php
/**
* Create widget for Akismet stats
*
* @package Akismet
*/
class Widget_Akismet extends WP_Widget {
function Widget_Akismet() {
$widget_ops = array( 'classname' => 'widget_akismet', 'description' => 'Akismet stat widget' );
$this->WP_Widget( 'akismet', 'Akismet', $widget_ops );
}
// Display the widget
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? 'Akismet' : $instance['title'] );
$count = get_option( 'akismet_spam_count' );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
?>
<div id="akismetwrap"><div id="akismetstats"><a id="aka" href="http://akismet.com" title=""><?php printf( _n( '%1$s%2$s%3$s %4$sspam comment%5$s %6$sblocked by%7$s<br />%8$sAkismet%9$s', '%1$s%2$s%3$s %4$sspam comments%5$s %6$sblocked by%7$s<br />%8$sAkismet%9$s', $count ), '<span id="akismet1"><span id="akismetcount">', number_format_i18n( $count ), '</span>', '<span id="akismetsc">', '</span></span>', '<span id="akismet2"><span id="akismetbb">', '</span>', '<span id="akismeta">', '</span></span>' ); ?></a></div></div>
<?php
echo $after_widget;
}
// Save widget settings
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
// Build Akismet widget settings form
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = strip_tags( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title' ); ?></label> <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<?php
}
}
// Register Akismet widget function
function akismet_widget_init() {
register_widget( 'Widget_Akismet' );
}
add_action( 'widgets_init', 'akismet_widget_init' );
// Register styles for akismet widget
function akismet_widget_style() {
$url = plugins_url( '/akismet-widget.css', __FILE__ );
wp_enqueue_style( 'akismet_widget', $url );
}
add_action( 'wp_enqueue_scripts', 'akismet_widget_style' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment