Simple Image Widget - Version 1.0

Version Description

Download this release

Release Info

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

Version 1.0

Files changed (3) hide show
  1. readme.txt +54 -0
  2. screenshot-1.png +0 -0
  3. simpleimage.php +201 -0
readme.txt ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Simple Image Widget ===
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: 2.6.2
7
+ Stable tag: 1.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. 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
+
17
+ * Image URL: The full URL to the image file
18
+ * Alternate Text: Shown by the browser if image cannot be displayed
19
+ * Link URL: URL to open when the image is clicked on (optional)
20
+ * Open link in new window: If this is checked, the above link URL will open in a new browser window
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
+
40
+ = How do I remove the border from around a link image? =
41
+
42
+ In your theme's CSS file, add this code:
43
+
44
+ `.simpleimage img { border: 0px; }`
45
+
46
+ = How do I change the alignment of the images? =
47
+
48
+ This can also be done with CSS:
49
+
50
+ `.simpleimage { text-align: center; }`
51
+
52
+ == Screenshots ==
53
+
54
+ 1. Simple Image Widget control interface
screenshot-1.png ADDED
Binary file
simpleimage.php ADDED
@@ -0,0 +1,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. Supports multiple instances, so you can use it multiple times in multiple sidebars.
5
+ Version: 1.0
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
15
+ the Free Software Foundation; either version 2 of the License, or
16
+ (at your option) any later version.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ */
27
+ ?>
28
+ <?
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
+ <?
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
+ ?>