Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Last active January 2, 2019 10:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gregrickaby/bb4870cb5fe723b156e6 to your computer and use it in GitHub Desktop.
Save gregrickaby/bb4870cb5fe723b156e6 to your computer and use it in GitHub Desktop.
Recent Posts with Images
<?php
/*
Plugin Name: Recent Posts with Images
Plugin URI: http://webdevstudios.com
Description: Display recent posts with images filterable by category or post type in a widget area.
Version: 1.0.0
Author: WebDevStudios
Author URI: http://webdevstudios.com
License: GPLv2
Text Domain: textdomain
*/
class Recent_Posts_with_Images extends WP_Widget {
/**
* Contruct widget
*/
public function __construct() {
parent::__construct(
'recent_posts_thumbnails', // Base ID
__( 'Recent Posts with Thumbnails', 'textdomain' ), // Name
array( 'description' => __( 'Display recent posts with thumbnails filterable by category or post type in a widget area.', 'textdomain' ) ) // Args
);
add_action( 'publish_post', array( $this, 'delete_transient_on_publish' ) );
}
/**
* Delete transient on post publish
*/
public function delete_transient_on_publish() {
delete_transient( $this->id );
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
// Get options
extract( $args );
$title = $instance['title'];
$ppp = $instance['post_count'];
$show_args = $instance['which_query'];
$show_img = $instance['show_img'];
$img_height = $instance['img_height'];
$img_width = $instance['img_width'];
echo $before_widget;
// Check for title
if ( isset( $title ) ) {
echo $before_title . sanitize_text_field( $title ) . $after_title;
}
// Check for transient
if ( false === ( $headlines = get_transient( $this->id ) ) ) {
if ( 'all' == $show_args ) {
// Execute WP Query
$headlines = new WP_Query( array(
'posts_per_page' => absint( $instance['post_count'] )
));
}
if ( 'cat' == $show_args ) {
// Execute WP Query
$headlines = new WP_Query( array(
'category_name' => esc_attr( $instance['category'] ),
'posts_per_page' => absint( $instance['post_count'] )
));
}
if ( 'cpt' == $show_args ) {
// Execute WP Query
$headlines = new WP_Query( array(
'post_type' => esc_attr( $instance['cpt'] ),
'posts_per_page' => absint( $instance['post_count'] )
));
}
// Store transient and expire after 4 hours
set_transient( $this->id, $headlines, 4 * HOUR_IN_SECONDS );
}
echo '<ul class="headlines">';
if ( $headlines->have_posts() ) {
while ( $headlines->have_posts() ) { $headlines->the_post();
// If 'display thumbnail' is enabled
if ( $show_img AND $show_img == true ) {
// Get post thumbnail attributes
$img_attr = wp_get_attachment_image_src( get_post_thumbnail_id( absint( get_the_ID() ) ), 'post-thumbnail' );
// If no image, set a default
if ( empty( $img_attr ) ) {
$img_src = plugins_url( 'img/stock-image-150x150.jpg' , __FILE__ );
} else {
$img_src = $img_attr[0];
}
// Write html
echo '<li style="height:' . absint( $img_height + 5 ) . 'px;"><a class="thumbnail-link" href="' . get_permalink() . '"><img src="' . esc_url( $img_src ) . '" height="' . absint( $img_height ) . '" width="' . absint( $img_width ) . '" style="float:left;height:' . absint( $img_height ) . 'px;margin-right:10px;width:' . absint( $img_width ) . 'px;vertical-align:middle;" alt="' . esc_html( get_the_title() ) . '" title="' . esc_html( get_the_title() ) . '"></a><a class="headline-link" href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
} else {
// If 'display thumbnail' is disabled, just write html
echo '<li><a class="headline-link" href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
}
} // endwhile
wp_reset_postdata();
} else {
echo __( 'Sorry, no posts found.', 'textdomain' );
}
echo '</ul>';
echo $after_widget;
}
/**
* Back-end widget form with defaults
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// Set default options
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Recent Posts',
'which_query' => 'cat',
'category' => 'Uncategorized',
'post_count' => '5',
'show_img' => 'on',
'img_width' => '50',
'img_height' => '50',
)); ?>
<p><label><?php _e( 'Title:', 'textdomain' ); ?></label>
<input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /></p>
<p><label for="<?php echo $this->get_field_name( 'which_query' ); ?>"><?php _e( 'Use:', 'textdomain' ); ?></label><br />
<input type="radio" id="<?php echo $this->get_field_id( 'which_query' ); ?>" name="<?php echo $this->get_field_name( 'which_query' ); ?>" <?php if( $instance['which_query'] == 'all') echo 'checked="checked"'; ?> value="all" />All Posts<br>
<input type="radio" id="<?php echo $this->get_field_id( 'which_query' ); ?>" name="<?php echo $this->get_field_name( 'which_query' ); ?>" <?php if( $instance['which_query'] == 'cat') echo 'checked="checked"'; ?> value="cat" />Category<br>
<input type="radio" id="<?php echo $this->get_field_id( 'which_query' ); ?>" name="<?php echo $this->get_field_name( 'which_query' ); ?>" <?php if( $instance['which_query'] == 'cpt') echo 'checked="checked"'; ?> value="cpt" />Post Type<br>
<p><label for="<?php echo $this->get_field_name( 'category' ); ?>"><?php _e( 'Category:', 'textdomain' ); ?></label></p>
<p><select class="widefat" name="<?php echo $this->get_field_name( 'category' ); ?>">
<?php
$categories = get_categories(); ?>
<option value="none">-- none --</option>
<?php foreach ( $categories as $category ) { ?>
<option value="<?php echo esc_attr( $category->cat_name ) ?>" <?php if ( $instance['category'] == $category->cat_name ) echo 'selected="selected"'; ?>><?php echo esc_attr( $category->cat_name ); ?></option>
<?php } ?>
</select></p>
<p><label for="<?php echo $this->get_field_name( 'cpt' ); ?>"><?php _e( 'Post Type:', 'textdomain' ); ?></label></p>
<p><select class="widefat" name="<?php echo $this->get_field_name( 'cpt' ); ?>">
<?php
$args = array( 'public' => true, '_builtin' => false );
$output = 'names';
$post_types = get_post_types( $args, $output ); ?>
<option value="none">-- none --</option>
<?php foreach ( $post_types as $post_type ) { ?>
<option value="<?php echo esc_attr( $post_type ) ?>" <?php if ( $instance['cpt'] == $post_type ) echo 'selected="selected"'; ?>><?php echo esc_attr( $post_type ); ?></option>
<?php } ?>
</select></p>
<p><label for="<?php echo $this->get_field_name( 'post_count' ); ?>"><?php _e( 'Number of posts to display:', 'textdomain' ); ?></label>
<select name="<?php echo $this->get_field_name( 'post_count' ); ?>">
<option value="1" <?php selected( $instance['post_count'], '1' ); ?>>1</option>
<option value="2" <?php selected( $instance['post_count'], '2' ); ?>>2</option>
<option value="3" <?php selected( $instance['post_count'], '3' ); ?>>3</option>
<option value="4" <?php selected( $instance['post_count'], '4' ); ?>>4</option>
<option value="5" <?php selected( $instance['post_count'], '5' ); ?>>5</option>
<option value="6" <?php selected( $instance['post_count'], '6' ); ?>>6</option>
</select></p>
<hr class="div">
<p><label for="<?php echo esc_attr( $this->get_field_id( 'show_img' ) ); ?>"><?php _e( 'Display thumbnail: ', 'textdomain' ); ?></label>
<input class="checkbox" type="checkbox" value ="1" <?php checked( '1', $show_img ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_img' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_img' ) ); ?>'); ?>" /></p>
<p>
<label for="<?php echo $this->get_field_id( 'img_width' ); ?>"><?php _e( 'Thumbnail size: ', 'textdomain' ) ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'img_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'img_width' ) ); ?>" type="text" size="3" value="<?php echo esc_attr( $instance['img_width'] ); ?>" style="padding: 1px; font-size: 12px; margin: 5px 0;" autocomplete="off" placeholder="width" />&#xd7;
<input id="<?php echo esc_attr( $this->get_field_id( 'img_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'img_height' ) ); ?>" type="text" size="3" value="<?php echo esc_attr( $instance['img_height'] );?>" style="padding: 1px; font-size: 12px; margin: 5px 0;" autocomplete="off" placeholder="height" />px</p>
<hr class="div">
<p style="margin:20px 0 0;" class="description"><?php _e( 'Use the "Widget Visibility" options below to select where this widget appears.', 'textdomain' ); ?></p>
<?php
}
/**
* Update form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
// Sanitize options
foreach ( array( 'title', 'which_query', 'category', 'cpt', 'post_count', 'show_img', 'img_width', 'img_height' ) as $key => $value ) {
$instance[$value] = sanitize_text_field( $new_instance[$value] );
}
// Delete transient on widget save
delete_transient( $this->id );
return $instance;
}
} // end Recent_Posts_with_Images
// Start the widget
add_action( 'widgets_init', function() { register_widget( 'Recent_Posts_with_Images' ); } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment