Advertisement
Digitalraindrops

Custom Post Widget

Mar 13th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Makes a custom Widget for displaying information posts
  4.  *
  5.  * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
  6.  *
  7.  * @package WordPress
  8.  * @subpackage Twenty_Eleven
  9.  * @since Twenty Eleven 1.0
  10.  */
  11. class Information_Widget extends WP_Widget {
  12.  
  13.     function Information_Widget() {
  14.         $widget_ops = array( 'classname' => 'information_widget', 'description' => __( 'Use this widget to display Information Posts', 'twentyeleven' ) );
  15.         $this->WP_Widget( 'information_widget', __( 'Information Posts', 'twentyeleven' ), $widget_ops );
  16.         $this->alt_option_name = 'information_widget';
  17.  
  18.         add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
  19.         add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
  20.         add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
  21.     }
  22.  
  23.     function widget( $args, $instance ) {
  24.         $cache = wp_cache_get( 'information_widget', 'widget' );
  25.  
  26.         if ( !is_array( $cache ) )
  27.             $cache = array();
  28.  
  29.         if ( ! isset( $args['widget_id'] ) )
  30.             $args['widget_id'] = null;
  31.  
  32.         if ( isset( $cache[$args['widget_id']] ) ) {
  33.             echo $cache[$args['widget_id']];
  34.             return;
  35.         }
  36.  
  37.         ob_start();
  38.         extract( $args, EXTR_SKIP );
  39.        
  40.         $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base);
  41.        
  42.         if ( ! isset( $instance['postid'] ) )
  43.             $postid = 0;
  44.  
  45.         if ( ! $postid = absint( $instance['postid'] ) )
  46.             $postid = 0;
  47.        
  48.         $thumbnail = ( isset( $instance['thumbnail'] ) ) ? $instance['thumbnail'] : false;
  49.         $excerpt = ( isset( $instance['excerpt'] ) ) ? $instance['excerpt'] : false;
  50.         // Set the global arguments
  51.         global $widget_args;
  52.         $widget_args=array();
  53.         $widget_args['postid'] = $postid;
  54.         $widget_args['thumbnail'] = $thumbnail;
  55.         $widget_args['excerpt'] = $excerpt;
  56.         $widget_args['uid'] = '-' .$args['widget_id'];
  57.        
  58.         // Display the infomation post
  59.         echo $before_widget;
  60.         echo $before_title;
  61.         echo $title; // Can set this with a widget option, or omit altogether
  62.         echo $after_title;     
  63.         get_template_part('information','html');
  64.         echo $after_widget;
  65.        
  66.         // Reset the post globals as this query will have stomped on it
  67.         wp_reset_postdata();
  68.        
  69.         $cache[$args['widget_id']] = ob_get_flush();
  70.        
  71.         wp_cache_set( 'information_widget', $cache, 'widget' );
  72.     }
  73.  
  74.     /**
  75.      * Deals with the settings when they are saved by the admin. Here is
  76.      * where any validation should be dealt with.
  77.      **/
  78.     function update( $new_instance, $old_instance ) {
  79.         $instance = $old_instance;
  80.         $instance['title'] = strip_tags( $new_instance['title'] );
  81.         $instance['postid'] = (int) $new_instance['postid'];
  82.         $instance['thumbnail'] = $new_instance['thumbnail'];
  83.         $instance['excerpt'] = $new_instance['excerpt'];
  84.         $this->flush_widget_cache();
  85.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  86.         if ( isset( $alloptions['information_widget'] ) )
  87.             delete_option( 'information_widget' );
  88.         return $instance;
  89.     }
  90.  
  91.     function flush_widget_cache() {
  92.         wp_cache_delete( 'information_widget', 'widget' );
  93.     }
  94.  
  95.     /**
  96.      * Displays the form for this widget on the Widgets page of the WP Admin area.
  97.      **/
  98.     function form( $instance ) {
  99.         $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
  100.         $postid = isset( $instance['postid'] ) ? absint( $instance['postid'] ) : 0;
  101.         $thumbnail = ( isset( $instance['thumbnail'] ) ) ? $instance['thumbnail'] : false;
  102.         $excerpt = isset( $instance['excerpt'] ) ? strip_tags( $instance['excerpt'] )  : false;
  103.         $postlist = dr_info_get_posts();
  104.     ?>
  105.         <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Information Title', 'twentyeleven' ); ?></label>
  106.         <br/>
  107.         <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
  108.  
  109.         <p><label for="<?php echo $this->get_field_id('postid'); ?>"><?php _e("Information Post Title"); ?></label>
  110.         <br/>
  111.         <select id="<?php echo $this->get_field_id('postid'); ?>" name="<?php echo $this->get_field_name('postid'); ?>">
  112.     <?php
  113.         foreach ($postlist as $output) :
  114.             $selected = ( $output['value'] == esc_attr($postid) ) ? ' selected = "selected" ' : '';
  115.             $option = '<option '.$selected .'value="' . $output['value'];
  116.             $option = $option .'">';
  117.             $option = $option .$output['label'];
  118.             $option = $option .'</option>';
  119.             echo $option;
  120.         endforeach;
  121.     ?>
  122.         </select></p>      
  123.        
  124.         <p><label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e("Hide Post Featured Image"); ?></label>
  125.         <br/>
  126.         <?php $checked = ( $thumbnail ) ? ' checked=\"checked\" ' : ''; ?>
  127.         <input type="checkbox" id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" value="true" <?php echo $checked; ?> />   
  128.         </p>
  129.        
  130.         <p><label for="<?php echo $this->get_field_id('excerpt'); ?>"><?php _e("Display Post Excerpt Only"); ?></label>
  131.         <br/>
  132.         <?php $checked = ( $excerpt ) ? ' checked=\"checked\" ' : ''; ?>
  133.         <input type="checkbox" id="<?php echo $this->get_field_id('excerpt'); ?>" name="<?php echo $this->get_field_name('excerpt'); ?>" value="true" <?php echo $checked; ?> />   
  134.         </p>   
  135.                
  136.         <?php
  137.     }
  138. }
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement