Random Post Image Widget

This is my first widget. This widget loads a random image from the media library and then displays it. The image can be linked to the post it is associated with. The post title can be shown below the image and the title above the image can be changed in the widget settings. This is not a very complex widget, but it does do something useful for me which is why I wrote it.

The code:

<?php /* Plugin Name: Random Photo Plugin URI: http://www.serenityrv.net/plugins/randphoto Description: Widget to display a random photo from the media library linked to its post Version: 0.1 Author: Daniel Smeltzer Author URI: http://www.codecompost.com License: Unknown */ add_action('widgets_init', 'ri_register_widget'); function ri_register_widget() {   register_widget('ri_widget'); } class ri_widget extends WP_Widget {   function ri_widget() {    $widget_ops = array('classname' => 'ri_widget', 'description' => 'Display random post image');
   $this->WP_Widget('ri_widget', 'Random Image Widget', $widget_ops);
  }</p> <p>  function form($instance) {</p> <p>   $instance = wp_parse_args((array) $instance, array('title' => 'Random Image', 'link_post'=>true, 'post_title'=>false));</p> <p>    $title = strip_tags($instance['title']);</p> <p>    ?></p> <p>     <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
    <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" id="<?php $this->get_field_id('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />     </p> <p>     <input type="checkbox" class="checkbox" name="<?php echo $this->get_field_name('link_post'); ?>" id="<?php echo $this->get_field_id('link_post'); ?>" <?php checked($instance['link_post'], true); ?> />
    <label for="<?php echo $this->get_field_id('link_post'); ?>">Link to post</label></p> <p>    <input type="checkbox" class="checkbox" name="<?php echo $this->get_field_name('post_title'); ?>" id="<?php echo $this->get_field_id('post_title'); ?>" <?php checked($instance['post_title'], true); ?> />
    <label for="<?php echo $this->get_field_id('post_title'); ?>">Show post title</label>     </p> <p>    <?php   }      function update($new_instance, $old_instance) {       $instance = array('link_post' => 0, 'post_title' => 0);</p> <p>   $instance['title'] = strip_tags(esc_attr($new_instance['title']));</p> <p>   if (isset($new_instance['link_post']))
   $instance['link_post'] = 1;</p> <p>   if (isset($new_instance['post_title']))
   $instance['post_title'] = 1;</p> <p>   return $instance;
  }</p> <p>  function widget($args, $instance) {
    global $post;
    extract($args);</p> <p>    echo $before_widget;</p> <p>    $title = apply_filters('widget_title', $instance['title']);</p> <p>    echo $before_title . $title . $after_title;</p> <p>    //$images =& get_children('post_type=attachment&numberposts=1&orderby=rand()');
    $args = array('post_type' => 'attachment', 'numberposts' => 1, 'post_parent' => null, 'post_mime_type' => 'image',  'orderby' => 'rand');
    $images = get_posts($args);
    if (empty($images)) {
     echo "not found";
    } else {
     foreach ($images as $image) {
     $parent = get_post($image->post_parent);
     if  ($instance['link_post']) {
     echo "<a href=\"" . get_permalink($parent->ID) . "\">";
     echo wp_get_attachment_image($image->ID);</p> <p>     if ($instance['post_title']) {
       echo "
" . $parent->post_title;
     }</p> <p>     if ($instance['link_post']) {
     echo "</a>";
    }</p> <p>    echo $after_widget;

Leave a Reply

Your email address will not be published. Required fields are marked *