Skip to content

Instantly share code, notes, and snippets.

@benbalter
Created January 5, 2012 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbalter/1566449 to your computer and use it in GitHub Desktop.
Save benbalter/1566449 to your computer and use it in GitHub Desktop.
Recently Answered Questions
<?php
/*
Plugin Name: Help - Recently Answered Questions
Description: Widget to display recently answers questions on help.hackshackers.com
Author: Benjamin J. Balter
Version: 1.0
Author URI: http://ben.balter.com
*/
class Recent_Answers_Widget extends WP_Widget {
//default settings
private $defaults = array(
'number' => 5,
'title' => 'Recent Answers',
);
/**
* Init widget and register
*/
function __construct() {
parent::WP_Widget( 'Recent_Answers_Widget', $name = 'Questions: Recently Answered' );
add_action( 'widgets_init', create_function( '', 'return register_widget("Recent_Answers_Widget");' ) );
}
/**
* Callback to display widget contents
*/
function widget( $args, $instance ) {
extract( $args );
$args = array(
'number' => $instance['number'],
'post_type' => 'answer',
);
$answers = get_posts( $args );
echo $before_widget;
echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title;
echo "<ol>";
foreach ( $answers as $answer ) {
$parent = get_post( $answer->post_parent );
$user = get_userdata( $answer->post_author );
echo '<li><a href="'. site_url( 'questions/user/' . $user->user_nicename . '/' ) . '" title="View ' . $user->display_name . '\'s profile">' . $user->display_name . '</a> on <a href="'. get_permalink( $answer->ID ) . '" title="View ' . get_the_title( $parent->ID ) . '">' . get_the_title( $parent->ID ) . "</a></li>";
}
echo "</ol>";
echo $after_widget;
}
/**
* Callback to display widget options form
*/
function form( $instance ) {
foreach ( $this->defaults as $key => $value )
if ( !isset( $instance[ $key ] ) )
$instance[ $key ] = $value;
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>">Number of Users</label><br />
<input class="small-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $instance['number']; ?>" />
</p>
<?php
}
/**
* Sanitizes options and saves
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];
return $instance;
}
}
new Recent_Answers_Widget;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment