Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active November 20, 2015 22:51
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 glueckpress/1629407 to your computer and use it in GitHub Desktop.
Save glueckpress/1629407 to your computer and use it in GitHub Desktop.
Makes a custom WordPress widget to display any published post.
<?php
// Make sure we have a theme domain for localization
if( ! defined( 'THEME_DOMAIN' ) )
define( 'THEME_DOMAIN', get_stylesheet() ); // Any translation has to go in your stylesheet directory
/**
* Makes a custom widget to display any published post.
*
* @package WordPress
*/
class Glueckpress_Single_Post_Widget extends WP_Widget {
/**
* Constructor
*
* @return void
**/
function Glueckpress_Single_Post_Widget() {
$widget_ops = array( 'classname' => 'widget_glueckpress_single_post', 'description' => __( 'Use this widget to display any single post from your published posts.', THEME_DOMAIN ) );
$this->WP_Widget( 'widget_glueckpress_single_post', __( 'Single Post', THEME_DOMAIN ), $widget_ops );
$this->alt_option_name = 'widget_glueckpress_single_post';
add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it's output
*/
function widget( $args, $instance ) {
global $post;
$cache = wp_cache_get( 'widget_glueckpress_single_post', 'widget' );
if ( !is_array( $cache ) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = null;
if ( isset( $cache[$args['widget_id']] ) ) {
echo $cache[$args['widget_id']];
return;
}
ob_start();
extract( $args, EXTR_SKIP );
// Widget settings
$post_id = isset( $instance['post_id'] ) ? $instance['post_id'] : 0;
$show_post_title = isset( $instance['show_post_title'] ) ? $instance['show_post_title'] : false;
$show_post_content = isset( $instance['show_post_content'] ) ? $instance['show_post_content'] : false;
$show_post_image = isset( $instance['show_post_image'] ) ? $instance['show_post_image'] : false;
$post = $content_post = get_post( $post_id );
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $before_widget;
if ( $show_post_title ) // Post title
echo $before_title . $content_post->post_title . $after_title . "\n"; // Post title
if ( $show_post_image ) : // Posts image ?>
<figure id="showcase-slider-image-<?php echo $post_id; ?>" class="showcase-slider-image">
<?php echo get_the_post_thumbnail( $post_id, array( FEATURED_IMAGE_WIDTH, FEATURED_IMAGE_HEIGHT ) ) . "\n"; ?>
</figure><?php
endif;
if ( $show_post_content )
echo $content . "\n"; // Post content
echo $after_widget;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set( 'widget_glueckpress_single_post', $cache, 'widget' );
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$title = strip_tags( $new_instance['title'] );
$instance['post_id'] = (int) $new_instance['post_id'];
$instance['show_post_title'] = (bool) $new_instance['show_post_title'];
$instance['show_post_content'] = (bool) $new_instance['show_post_content'];
$instance['show_post_image'] = (bool) $new_instance['show_post_image'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['widget_glueckpress_single_post'] ) )
delete_option( 'widget_glueckpress_single_post' );
return $instance;
}
function flush_widget_cache() {
wp_cache_delete( 'widget_glueckpress_single_post', 'widget' );
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*/
function form( $instance ) {
$title = empty( $instance['post_id'] ) ? __( 'Single Post', THEME_DOMAIN ) : get_the_title( $instance['post_id'] );
$post_id = isset( $instance['post_id'] ) ? $instance['post_id'] : 0;
$show_post_title = isset( $instance['show_post_title'] ) ? $instance['show_post_title'] : true;
$show_post_content = isset( $instance['show_post_content'] ) ? $instance['show_post_content'] : true;
$show_post_image = isset( $instance['show_post_image'] ) ? $instance['show_post_image'] : true;
?>
<p>
<label for="<?php echo $this->get_field_id('post_id'); ?>"><?php echo __( 'Display Post:', THEME_DOMAIN ) ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>">
<?php
$wp_query = new WP_Query( array(
'post_type' => 'post',
'showposts' => -1,
'post_status' => 'publish'
));
if ( $wp_query->have_posts() ) { // begin have_posts() statement
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$current_id = get_the_ID();
$extra = $current_id == $post_id ? 'selected' : '';
echo '<option value="' . $current_id . '" ' .$extra . '>' . get_the_title() . '</option>';
}
} else { // continuing have_posts() statement
echo '<option value="empty">' . __( 'No posts available.', THEME_DOMAIN ) . '</option>';
} // end have_posts() statement
?>
</select>
</p>
<input type="hidden" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" />
<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_title, true ); ?> id="<?php echo $this->get_field_id( 'show_post_title' ); ?>" name="<?php echo $this->get_field_name( 'show_post_title' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_post_title' ); ?>"><?php echo __( 'Display title', THEME_DOMAIN ) ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_content, true ); ?> id="<?php echo $this->get_field_id( 'show_post_content' ); ?>" name="<?php echo $this->get_field_name( 'show_post_content' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_post_content' ); ?>"><?php echo __( 'Display content', THEME_DOMAIN ) ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_image, true ); ?> id="<?php echo $this->get_field_id( 'show_post_image' ); ?>" name="<?php echo $this->get_field_name( 'show_post_image' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_post_image' ); ?>"><?php echo __( 'Display featured image', THEME_DOMAIN ) ?></label>
</p><?php
wp_reset_query();
}
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment