Simple Image Widget - Version 2.0

Version Description

Download this release

Release Info

Developer vickio
Plugin Icon 128x128 Simple Image Widget
Version 2.0
Comparing to
See all releases

Code changes from version 1.3 to 2.0

Files changed (3) hide show
  1. readme.txt +13 -11
  2. screenshot-1.png +0 -0
  3. simpleimage.php +65 -142
readme.txt CHANGED
@@ -2,15 +2,15 @@
2
  Contributors: vickio
3
  Donate link: http://vickio.net
4
  Tags: image, sidebar, widget, photo, picture
5
- Requires at least: 2.5
6
- Tested up to: 3.0
7
- Stable tag: 1.3
8
 
9
  The simple way to place images in your sidebars.
10
 
11
  == Description ==
12
 
13
- Using this widget you can easily place an image in the sidebar. You can also specify a URL to link to when clicking on the image. Supports multiple instances, so you can use it multiple times in multiple sidebars.
14
 
15
  Once the plugin is enabled, the widget will be available in your widgets list as "Simple Image". You can add this widget to sidebars as many times as you need. The control interface allows you to specify the following options for each instance of the widget:
16
 
@@ -21,19 +21,17 @@ Once the plugin is enabled, the widget will be available in your widgets list as
21
 
22
  == Installation ==
23
 
24
- Installation is very simple:
25
-
26
- 1. Copy/upload the `simple-image-widget` folder to your `/wp-content/plugins/` directory
27
- 1. Activate the plugin through the 'Plugins' menu in WordPress
28
- 1. Add the "Simple Image" plugin to a sidebar in 'Design' -> 'Widgets'
29
 
30
  == Frequently Asked Questions ==
31
 
32
  = How do I upload an image? =
33
 
34
- The Simple Image widget does not provide a mechanism for uploading images or files. You can however upload an image in the 'Write' section of Wordpress. From there you can click on the 'Add an Image' icon (next to 'Add Media' label). After uploading an image, copy the 'Link URL' for use in your Simple Image widget.
35
 
36
- = How many images can be display? =
37
 
38
  Each instance of the widget can only display one image, but you can create as many instances as you need.
39
 
@@ -49,6 +47,10 @@ This can also be done with CSS:
49
 
50
  `.simpleimage { text-align: center; }`
51
 
 
 
 
 
52
  == Screenshots ==
53
 
54
  1. Simple Image Widget control interface
2
  Contributors: vickio
3
  Donate link: http://vickio.net
4
  Tags: image, sidebar, widget, photo, picture
5
+ Requires at least: 2.8
6
+ Tested up to: 3.1.1
7
+ Stable tag: 2.0
8
 
9
  The simple way to place images in your sidebars.
10
 
11
  == Description ==
12
 
13
+ Using this widget you can easily place an image in the sidebar. You can also specify a URL to link to when clicking on the image. It supports multiple instances, so you can use it multiple times in multiple sidebars.
14
 
15
  Once the plugin is enabled, the widget will be available in your widgets list as "Simple Image". You can add this widget to sidebars as many times as you need. The control interface allows you to specify the following options for each instance of the widget:
16
 
21
 
22
  == Installation ==
23
 
24
+ 1. Copy or upload the `simple-image-widget` folder to your `/wp-content/plugins/` directory
25
+ 1. Activate the plugin on the Plugins page
26
+ 1. Add the "Simple Image" widget to a sidebar in Appearance -> Widgets
 
 
27
 
28
  == Frequently Asked Questions ==
29
 
30
  = How do I upload an image? =
31
 
32
+ The Simple Image widget does not provide a mechanism for uploading images or files. You can however easily upload an image by selecting Media -> Add New. After uploading an image, copy its 'File URL' for use in the Simple Image widget 'Image URL' field.
33
 
34
+ = How many images can be displayed? =
35
 
36
  Each instance of the widget can only display one image, but you can create as many instances as you need.
37
 
47
 
48
  `.simpleimage { text-align: center; }`
49
 
50
+ = How do I edit my theme's CSS file? =
51
+
52
+ In Wordpress, select Appearance -> Editor, then in the file list on the right, select Stylesheet (style.css) under Styles. Add your custom CSS code to the bottom of this file.
53
+
54
  == Screenshots ==
55
 
56
  1. Simple Image Widget control interface
screenshot-1.png CHANGED
Binary file
simpleimage.php CHANGED
@@ -1,14 +1,14 @@
1
  <?php
2
  /*
3
  Plugin Name: Simple Image Widget
4
- Description: Using this widget you can easily place an image and link in the sidebar. Supports multiple instances, so you can use it multiple times in multiple sidebars.
5
- Version: 1.3
6
  Author: Chris Vickio
7
  Author URI: http://vickio.net
8
  */
9
  ?>
10
  <?php
11
- /* Copyright 2008 Chris Vickio (email : chris@vickio.net)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
@@ -26,176 +26,99 @@ Author URI: http://vickio.net
26
  */
27
  ?>
28
  <?php
29
- // Displays an image in the sidebar
30
- // $widget_args: number
31
- // number: which of the several widgets of this type do we mean
32
- function widget_simpleimage( $args, $widget_args = 1 ) {
33
- extract( $args, EXTR_SKIP );
34
- if ( is_numeric($widget_args) )
35
- $widget_args = array( 'number' => $widget_args );
36
- $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
37
- extract( $widget_args, EXTR_SKIP );
38
-
39
- // Data is stored as array: array( number => data for that instance of the widget, ... )
40
- $options = get_option('widget_simpleimage');
41
- if ( !isset($options[$number]) )
42
- return;
43
-
44
- if ($options[$number]['link']) {
45
- if ($options[$number]['new_window'])
46
- $before_image = "<a href=\"".$options[$number]['link']."\" target=\"_blank\">";
47
- else
48
- $before_image = "<a href=\"".$options[$number]['link']."\">";
49
 
50
- $after_image = "</a>";
51
- }
52
-
53
- if ($options[$number]['image'])
54
- $title = preg_replace('/\?.*/', "", basename($options[$number]['image']));
55
-
56
- ?>
57
- <?php echo $before_widget; ?>
58
- <div class="simpleimage">
59
- <?php // Using HTML comments here, the admin interface is tricked into displaying the title, but it's not actually displayed on the site ?>
60
- <?php if ( !empty( $title ) ) { echo "<!-- Control Title: " . $before_title . $title . $after_title . " -->"; } ?>
61
- <?php echo $before_image; ?>
62
- <p><img src="<?php echo $options[$number]['image']; ?>" alt="<?php echo $options[$number]['alt']; ?>" /></p>
63
- <?php echo $after_image; ?>
64
- </div>
65
- <?php echo $after_widget; ?>
66
- <?php
67
- }
68
 
69
- // Displays form for image and link. Also updates the data after a POST submit
70
- // $widget_args: number
71
- // number: which of the several widgets of this type do we mean
72
- function widget_simpleimage_control( $widget_args = 1 ) {
73
- global $wp_registered_widgets;
74
- static $updated = false; // Whether or not we have already updated the data after a POST submit
75
-
76
- if ( is_numeric($widget_args) )
77
- $widget_args = array( 'number' => $widget_args );
78
- $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
79
- extract( $widget_args, EXTR_SKIP );
80
-
81
- // Data is stored as array: array( number => data for that instance of the widget, ... )
82
- $options = get_option('widget_simpleimage');
83
- if ( !is_array($options) )
84
- $options = array();
85
-
86
- // We need to update the data
87
- if ( !$updated && !empty($_POST['sidebar']) ) {
88
- // Tells us what sidebar to put the data in
89
- $sidebar = (string) $_POST['sidebar'];
90
-
91
- $sidebars_widgets = wp_get_sidebars_widgets();
92
- if ( isset($sidebars_widgets[$sidebar]) )
93
- $this_sidebar =& $sidebars_widgets[$sidebar];
94
- else
95
- $this_sidebar = array();
96
-
97
- foreach ( $this_sidebar as $_widget_id ) {
98
- if ( 'widget_simpleimage' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
99
- $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
100
- if ( !in_array( "simpleimage-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed
101
- unset($options[$widget_number]);
102
- }
103
- }
104
-
105
- foreach ( (array) $_POST['widget-simpleimage'] as $widget_number => $widget_simpleimage ) {
106
- if ( !isset($widget_simpleimage['image']) && isset($options[$widget_number]) ) // user clicked cancel
107
- continue;
108
-
109
- $image = wp_specialchars( $widget_simpleimage['image'] );
110
- $alt = wp_specialchars( $widget_simpleimage['alt'] );
111
- $link = wp_specialchars( $widget_simpleimage['link'] );
112
- $new_window = isset( $widget_simpleimage['new_window'] );
113
- $options[$widget_number] = compact('image', 'alt', 'link', 'new_window');
114
- }
115
-
116
- update_option('widget_simpleimage', $options);
117
- $updated = true; // So that we don't go through this more than once
118
  }
119
 
 
 
 
120
 
121
- // Here we echo out the form
122
- if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
123
- $image = '';
124
- $alt = '';
125
- $link = '';
126
- $new_window = '';
127
- $number = '%i%';
128
- } else {
129
- $image = attribute_escape($options[$number]['image']);
130
- $alt = attribute_escape($options[$number]['alt']);
131
- $link = attribute_escape($options[$number]['link']);
132
- $new_window = attribute_escape($options[$number]['new_window']);
 
 
 
 
 
133
  }
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  ?>
 
 
 
136
  <p>
137
- <label for="simpleimage-image-<?php echo $number; ?>">
138
  <?php _e('Image URL:'); ?>
139
- <input class="widefat" id="simpleimage-image-<?php echo $number; ?>" name="widget-simpleimage[<?php echo $number; ?>][image]" type="text" value="<?php echo $image; ?>" />
140
  </label>
141
  </p>
142
 
143
  <p>
144
- <label for="simpleimage-alt-<?php echo $number; ?>">
145
  <?php _e('Alternate Text:'); ?>
146
- <input class="widefat" id="simpleimage-alt-<?php echo $number; ?>" name="widget-simpleimage[<?php echo $number; ?>][alt]" type="text" value="<?php echo $alt; ?>" />
147
  <br />
148
  <small><?php _e( 'Shown if image cannot be displayed' ); ?></small>
149
  </label>
150
  </p>
151
 
152
  <p>
153
- <label for="simpleimage-link-<?php echo $number; ?>">
154
  <?php _e('Link URL (optional):'); ?>
155
- <input class="widefat" id="simpleimage-link-<?php echo $number; ?>" name="widget-simpleimage[<?php echo $number; ?>][link]" type="text" value="<?php echo $link; ?>" />
156
  </label>
157
  </p>
158
 
159
  <p>
160
- <label for="simpleimage-new-window-<?php echo $number; ?>">
161
- <input id="simpleimage-new-window-<?php echo $number; ?>" name="widget-simpleimage[<?php echo $number; ?>][new_window]" type="checkbox" <?php if ($new_window) echo 'checked="checked"'; ?> />
162
  <?php _e('Open link in new window'); ?>
163
  </label>
164
  </p>
165
-
166
- <input type="hidden" id="widget-simpleimage-submit-<?php echo $number; ?>" name="widget-simpleimage[<?php echo $number; ?>][submit]" value="1" />
167
  <?php
168
- }
169
-
170
- // Registers each instance of widget on startup
171
- function widget_simpleimage_register() {
172
- if ( !$options = get_option('widget_simpleimage') )
173
- $options = array();
174
-
175
- $widget_ops = array('classname' => 'widget_simpleimage', 'description' => __('Display an image'));
176
- $control_ops = array( 'id_base' => 'simpleimage');
177
- $name = __('Simple Image');
178
-
179
- $registered = false;
180
- foreach ( array_keys($options) as $o ) {
181
- // Old widgets can have null values for some reason
182
- if ( !isset($options[$o]['image']) )
183
- continue;
184
-
185
- $id = "simpleimage-$o"; // Never never never translate an id
186
- $registered = true;
187
- wp_register_sidebar_widget( $id, $name, 'widget_simpleimage', $widget_ops, array( 'number' => $o ) );
188
- wp_register_widget_control( $id, $name, 'widget_simpleimage_control', $control_ops, array( 'number' => $o ) );
189
- }
190
-
191
- // If there are none, we register the widget's existance with a generic template
192
- if ( !$registered ) {
193
- wp_register_sidebar_widget( 'simpleimage-1', $name, 'widget_simpleimage', $widget_ops, array( 'number' => -1 ) );
194
- wp_register_widget_control( 'simpleimage-1', $name, 'widget_simpleimage_control', $control_ops, array( 'number' => -1 ) );
195
  }
196
  }
197
 
198
- // Hook for the registration
199
- add_action( 'widgets_init', 'widget_simpleimage_register' )
 
 
 
200
 
201
- ?>
1
  <?php
2
  /*
3
  Plugin Name: Simple Image Widget
4
+ Description: Using this widget you can easily place an image and link in the sidebar. It supports multiple instances, so you can use it multiple times in multiple sidebars.
5
+ Version: 2.0
6
  Author: Chris Vickio
7
  Author URI: http://vickio.net
8
  */
9
  ?>
10
  <?php
11
+ /* Copyright 2011 Chris Vickio (email : chris@vickio.net)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
26
  */
27
  ?>
28
  <?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ // Inspired by the built-in WP_Widget_Text class
31
+ class WP_Widget_Simple_Image extends WP_Widget {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ // Set up the widget classname and description
34
+ function WP_Widget_Simple_Image() {
35
+ $widget_ops = array('classname' => 'widget_simpleimage', 'description' => __('Display an image'));
36
+ $control_ops = array();
37
+ $this->WP_Widget('simpleimage', __('Simple Image'), $widget_ops, $control_ops);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
39
 
40
+ // Display the widget on the web site
41
+ function widget( $args, $instance ) {
42
+ extract($args);
43
 
44
+ // Optionally generate the link code
45
+ if ($instance['link']) {
46
+ if ($instance['new_window'])
47
+ $before_image = "<a href=\"".$instance['link']."\" target=\"_blank\">";
48
+ else
49
+ $before_image = "<a href=\"".$instance['link']."\">";
50
+
51
+ $after_image = "</a>";
52
+ }
53
+
54
+ echo $before_widget; ?>
55
+ <div class="simpleimage">
56
+ <?php echo $before_image; ?>
57
+ <img src="<?php echo $instance['image']; ?>" alt="<?php echo $instance['alt']; ?>" />
58
+ <?php echo $after_image; ?>
59
+ </div>
60
+ <?php echo $after_widget;
61
  }
62
 
63
+ // Save the settings for this instance
64
+ function update( $new_instance, $old_instance ) {
65
+ $instance = $old_instance;
66
+ $instance['image'] = strip_tags($new_instance['image']);
67
+ $instance['alt'] = strip_tags($new_instance['alt']);
68
+ $instance['link'] = strip_tags($new_instance['link']);
69
+ $instance['new_window'] = isset($new_instance['new_window']);
70
+ return $instance;
71
+ }
72
+
73
+ // Display the widget form in the admin interface
74
+ function form( $instance ) {
75
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'image' => '', 'alt' => '', 'link' => '', 'new_window' => false ) );
76
+
77
+ // Generate a title based on the image URL
78
+ if ($instance['image'])
79
+ $title = preg_replace('/\?.*/', "", basename($instance['image']));
80
+
81
  ?>
82
+ <?php // Hidden title field for the admin interface to display ?>
83
+ <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="hidden" value="<?php echo $title; ?>" />
84
+
85
  <p>
86
+ <label for="<?php echo $this->get_field_id('image'); ?>">
87
  <?php _e('Image URL:'); ?>
88
+ <input class="widefat" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="text" value="<?php echo $instance['image']; ?>" />
89
  </label>
90
  </p>
91
 
92
  <p>
93
+ <label for="<?php echo $this->get_field_id('alt'); ?>">
94
  <?php _e('Alternate Text:'); ?>
95
+ <input class="widefat" id="<?php echo $this->get_field_id('alt'); ?>" name="<?php echo $this->get_field_name('alt'); ?>" type="text" value="<?php echo $instance['alt']; ?>" />
96
  <br />
97
  <small><?php _e( 'Shown if image cannot be displayed' ); ?></small>
98
  </label>
99
  </p>
100
 
101
  <p>
102
+ <label for="<?php echo $this->get_field_id('link'); ?>">
103
  <?php _e('Link URL (optional):'); ?>
104
+ <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $instance['link']; ?>" />
105
  </label>
106
  </p>
107
 
108
  <p>
109
+ <label for="<?php echo $this->get_field_id('new_window'); ?>">
110
+ <input id="<?php echo $this->get_field_id('new_window'); ?>" name="<?php echo $this->get_field_name('new_window'); ?>" type="checkbox" <?php if ($instance['new_window']) echo 'checked="checked"'; ?> />
111
  <?php _e('Open link in new window'); ?>
112
  </label>
113
  </p>
 
 
114
  <?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  }
116
  }
117
 
118
+ // Init function for registering the widget
119
+ function widget_simpleimage_init() {
120
+ register_widget('WP_Widget_Simple_Image');
121
+ }
122
+ add_action('init', 'widget_simpleimage_init', 1);
123
 
124
+ ?>