Skip to content

Instantly share code, notes, and snippets.

@aramk
Last active December 10, 2015 13:28
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 aramk/4440762 to your computer and use it in GitHub Desktop.
Save aramk/4440762 to your computer and use it in GitHub Desktop.
A Wordpress widget base class which reduces the amount of code needed to create a simple widget with fields. Allows overriding a method adding more complex fields. This code should be placed in your theme's function.php file. You can then subclass Field_Widget whenever you like by following the example given.
/**
* A Wordpress widget base class which reduces the amount of code needed to create a simple widget with fields. Allows overriding a method adding more complex fields.
*/
class Field_Widget extends WP_Widget {
private $fields = array();
private $defaults = array();
private $names = array();
function update($new_instance, $old_instance) {
$instance = $old_instance;
foreach ($new_instance as $field => $value) {
$instance[$field] = strip_tags($value);
}
return $instance;
}
/**
* Delegates the content of the widget to widget_content() and prints the title and surrounding content
* Override this if needed for more customisation.
*
* @param $args
* @param $instance
*/
function widget($args, $instance) {
echo $args['before_widget'];
echo $args['before_title'];
$this->widget_title($args, $instance);
echo $args['after_title'];
$this->widget_content($args, $instance);
echo $args['after_widget'];
}
function widget_title($args, $instance) {
echo $args['widget_name'];
}
/**
* Used to print the widget content
*
* @param $args
* @param $instance
*/
function widget_content($args, $instance) {
// Override!
}
/**
* Sets form fields.
*
* @param $fields An associative array of field ids to either values or an array with
* the first element as the field name and the second as the field value
*/
function set_fields($fields) {
$this->fields = $fields;
foreach ($fields as $field => $value) {
if (is_array($value)) {
$this->names[$field] = $value[0];
$this->defaults[$field] = $value[1];
} else {
$this->names[$field] = ucwords($value);
$this->defaults[$field] = $value;
}
}
}
function get_defaults() {
return $this->defaults;
}
function get_fields() {
return $this->fields;
}
function get_names() {
return $this->names;
}
/**
* Called for each form field encountered in form().
* Override and call parent::form_field() in subclasses when default behaviour is needed.
*
* @param $field The id of the form field defined with set_fields()
* @param $name The name of the form field defined with set_fields()
* @param $value The value of field from the db; blank on first submit
* @param $id The generated form field id
*/
function form_field($field, $name, $value, $id) {
?>
<p><label for="<?php echo $id ?>"><?php echo $name; ?>:</label>
<input class="widefat" id="<?php echo $id; ?>"
name="<?php echo $this->get_field_name($field); ?>"
type="text"
value="<?php echo attribute_escape($value); ?>"/>
</p>
<?php
}
/**
* Overridden form function which delegates drawing the form to form_field()
*/
function form($instance) {
$defaults = self::get_defaults();
$names = self::get_names();
$instanceArray = wp_parse_args((array)$instance, $defaults);
foreach (array_keys($defaults) as $field) {
$name = isset($names[$field]) ? $names[$field] : ucwords($field);
$value = strip_tags($instanceArray[$field]);
$id = $this->get_field_id($field);
self::form_field($field, $name, $value, $id);
}
}
}
/**
* Widget which allows choosing a page or post and writing a few short descriptions about it.
* The widget then presents the thumbnail image and the descriptions.
*/
class FeaturedPost extends Field_Widget {
function __construct() {
parent::__construct(false, 'Featured Post', array(
'description' => 'Displays the featured image and descriptions for a post'
));
self::set_fields(array(
'post' => array(__('Post'), ''),
'description' => array(__('Description'), ''),
'small-description' => array(__('Small Description'), '')
));
}
function widget($args, $instance) {
$id = intval($instance['post']);
$desc = $instance['description'];
$smallDesc = $instance['small-description'];
$post = get_post($id);
if ($post) {
echo '<div class="featured-post">';
echo '<div class="header"><h3>' . get_the_title($id) . '</h3></div>';
$thumbnail = get_the_post_thumbnail($id, array(275, 207));
echo '<div class="thumbnail">' . $thumbnail . '</div>';
if ($desc) {
echo '<div class="description">' . $desc . '</div>';
}
if ($smallDesc) {
echo '<div class="small-description">' . $smallDesc . '</div>';
}
echo '</div>';
}
}
function form_field($field, $name, $value, $id) {
if ($field == 'post') {
?>
<p>
<label for="<?php echo $id ?>"><?php echo $name; ?>:</label>
<select class="widefat" id="<?php echo $id; ?>" name="<?php echo $this->get_field_name($field); ?>">
<?php
$posts = get_all_posts();
foreach ($posts as $post) {
echo '<option value="' . $post->ID . '" ' . selected($value, $post->ID, false) . '>' . get_the_title($post->ID) . '</option>';
}
?>
</select>
</p>
<?php
} else {
parent::form_field($field, $name, $value, $id);
}
}
}
register_widget('FeaturedPost');
/**
* Widget which shows the last several posts with thumbnails
*/
class ThumbnailPosts extends Field_Widget {
function __construct() {
parent::__construct(false, 'Thumbnail Posts', array(
'description' => 'Displays the last several posts with thumbnails'
));
self::set_fields(array(
'title' => array(__('Title'), ''),
'limit' => array(__('Limit'), 2)
));
}
function widget_content($args, $instance) {
$limit = intval($instance['post']);
$posts = get_posts(array(
'post_type' => 'post',
'orderby' => 'post_date',
'limit' => $limit
));
foreach ($posts as $post) {
$id = $post->ID;
echo '<div class="thumbnail-post">';
echo '<div class="header"><h4>' . get_the_title($id) . '</h4></div>';
$thumbnail = get_the_post_thumbnail($id, array(275, 207));
echo '<div class="thumbnail">' . $thumbnail . '</div>';
echo '</div>';
}
}
function widget_title($args, $instance) {
echo $instance['title'];
}
function form_field($field, $name, $value, $id) {
if ($field == 'post') {
?>
<p>
<label for="<?php echo $id ?>"><?php echo $name; ?>:</label>
<select class="widefat" id="<?php echo $id; ?>" name="<?php echo $this->get_field_name($field); ?>">
<?php
$posts = array_merge(get_posts(array(
'orderby' => 'post_date',
'post_type' => 'any'
)), get_posts(array(
'orderby' => 'post_date',
'post_type' => 'post'
)));
foreach ($posts as $post) {
echo '<option value="' . $post->ID . '" ' . selected($value, $post->ID, false) . '>' . get_the_title($post->ID) . '</option>';
}
?>
</select>
</p>
<?php
} else {
parent::form_field($field, $name, $value, $id);
}
}
}
register_widget('ThumbnailPosts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment