amr shortcode any widget - Version 1.7

Version Description

Download this release

Release Info

Developer anmari
Plugin Icon wp plugin amr shortcode any widget
Version 1.7
Comparing to
See all releases

Code changes from version 1.6 to 1.7

Files changed (2) hide show
  1. amr-shortcode-any-widget.php +316 -0
  2. readme.txt +9 -4
amr-shortcode-any-widget.php ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: amr shortcode any widget
4
+ Plugin URI: http://webdesign.anmari.com/shortcode-any-widget/
5
+ Description: Include any widget in a page for any theme. [do_widget widgetname ] or [do_widget "widget name" ]. If upgrading see changelog. Can be very powerful eg: with queryposts widget it can become a templater.
6
+ Author: anmari
7
+ Version: 1.7
8
+ Author URI: http://webdesign.anmari.com
9
+
10
+ */
11
+
12
+ /*-----------------------------------*/
13
+ function do_widget($atts) {
14
+
15
+ global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars;
16
+
17
+ /* check if the widget is in the shortcode x sidebar if not , just use generic,
18
+ if it is in, then get the instance data and use that */
19
+
20
+ if (isset($_wp_sidebars_widgets) ) {
21
+ amr_show_widget_debug('which one'); //check for debug prompt and show widgets in shortcode sidebar if requested and logged in etc
22
+ }
23
+ else {
24
+ echo '<br />No widgets defined at all in any sidebar!';
25
+ return (false);
26
+ }
27
+
28
+ extract(shortcode_atts(array(
29
+ 'sidebar' => 'Shortcodes',
30
+ 'id' => '',
31
+ 'title' => 'true' /* do the default title unless they ask us not to - use string here not boolean */
32
+ ), $atts));
33
+
34
+
35
+
36
+ /* the widget need not be specified, [do_widget widgetname] is adequate */
37
+ if (!empty($atts[0])) { // we have a name
38
+ $widget = $atts[0];
39
+
40
+ foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
41
+ if (strtolower($w['name']) === strtolower($widget)) $widget_ids[] = $i;
42
+ //if ($debug) {echo '<br /> Check: '.$w['name'];}
43
+ }
44
+
45
+ }
46
+ else { /* check for id if we do not have a name */
47
+ if (!empty($id)) { /* if a specific id has been specified */
48
+
49
+ foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
50
+ if ($w['id'] === $id) $widget_ids[] = $id;
51
+ }
52
+ //if ($debug) { echo '<h2>We have an id: '.$id.'</h2>'; if (!empty($widget_ids)) var_dump($widget_ids); }
53
+ }
54
+ else {
55
+ echo '<br />No valid widget name or id given in shortcode parameters';
56
+ return (false);
57
+ }
58
+ }
59
+
60
+ if (empty ($widget_ids)) {
61
+ echo '<p><b>Requested widget not found in widget list.';
62
+ amr_show_widget_debug('empty', $atts);
63
+ return (false) ;
64
+ }
65
+
66
+ if ($title == 'false')
67
+ $title = false; /* If ask not to display title, then do not */
68
+ else
69
+ $title = true;
70
+
71
+ if (!($sidebarid = get_sidebar_id ($sidebar)))
72
+ $sidebarid=$sidebar; /* get the official sidebar id - will take the first one */
73
+
74
+ if (empty($widget))
75
+ $widget = '';
76
+ /* if ($debug) {
77
+ echo '<hr>Looking for widget with name:'.$widget.' or id='.$id.' Found instances:'.' <br />';
78
+ if (!empty($widget_ids)) foreach ($widget_ids as $i=> $w) {
79
+ echo $w.'<br />';
80
+ };
81
+ }
82
+ */
83
+ $content = '';
84
+ /* if the widget is in our chosen sidebar, then use the options stored for that */
85
+
86
+ if ((isset ($_wp_sidebars_widgets[$sidebarid])) and (!empty ($_wp_sidebars_widgets[$sidebarid]))) {
87
+ /* if ($debug) {
88
+ echo '<br />Widget ids in sidebar: "'.$sidebar.'" with id: '.$sidebarid .'<br />';
89
+ sort ($_wp_sidebars_widgets[$sidebarid]);
90
+ foreach ($_wp_sidebars_widgets[$sidebarid] as $i=> $w) {
91
+ echo $i.' '.$w.'<br />';
92
+ };
93
+ }
94
+ */
95
+ /* get the intersect of the 2 widget setups so we just get the widget we want */
96
+
97
+ $wid = array_intersect ($_wp_sidebars_widgets[$sidebarid], $widget_ids );
98
+ /* if ($debug) { echo '<br />Will use widget ids'.'<br />';
99
+ foreach ($widget_ids as $i=> $w) {
100
+ echo '&nbsp;&nbsp;&nbsp;'.$w.'<br />';
101
+ };
102
+ }
103
+ */
104
+ }
105
+ else { /* the sidebar is not defined */
106
+ //if ($debug) {
107
+ echo '<br />Sidebar '.$sidebar.'with sidebarid '.$sidebarid.' is empty or not defined.';
108
+ //}
109
+ }
110
+
111
+ $output = '';
112
+ if (empty ($wid) or (!is_array($wid)) or (count($wid) < 1)) {
113
+ //if ($debug) {
114
+ echo '<h2>Widget '.$widget.' not in sidebar with id '.$sidebarid.' and with name '.$sidebar.'</h2>';
115
+ //}
116
+ unset($sidebar);
117
+ unset($sidebarid);
118
+
119
+ }
120
+ else {
121
+ /* There may only be one but if we have two in our chosen widget then it will do both */
122
+ $output = '';
123
+ foreach ($wid as $i=>$widget_instance) {
124
+ ob_start(); /* catch the echo output, so we can control where it appears in the text */
125
+ shortcode_sidebar($widget_instance, $sidebar, $title);
126
+ $output .= ob_get_clean();
127
+ }
128
+
129
+ }
130
+
131
+ return ($output);
132
+ }
133
+ /*-----------------------------------*/
134
+ function amr_get_sidebar_name ($id) { /* dont need anymore ? or at least temporarily */
135
+ /* walk through the registered sidebars with a name and find the id - will be something like sidebar-integer. take the first one */
136
+ global $wp_registered_sidebars;
137
+ foreach ($wp_registered_sidebars as $i => $a) {
138
+ if ((isset ($a['id'])) and ( $a['id'] === $id)) {
139
+ if (isset($a['name'])) return ($a['name']);
140
+ else return ($id);
141
+ }
142
+ }
143
+ return (false);
144
+ }
145
+ /*-----------------------------------*/
146
+ function get_sidebar_id ($name) { /* dont need anymore ? or at least temporarily */
147
+ /* walk through the registered sidebars with a name and find the id - will be something like sidebar-integer. take the first one */
148
+ global $wp_registered_sidebars;
149
+ foreach ($wp_registered_sidebars as $i => $a) {
150
+ if ((isset ($a['name'])) and ( $a['name'] === $name)) return ($i);
151
+ }
152
+ return (false);
153
+ }
154
+ /* -------------------------------------------------------------------------*/
155
+ function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically the wordpress code, slightly modified */
156
+ global $wp_registered_sidebars, $wp_registered_widgets;
157
+
158
+ $debug = amr_check_if_widget_debug();
159
+
160
+ if ( is_int($index) ) {
161
+ $index = "sidebar-$index";
162
+ } else {
163
+ $index = sanitize_title($index);
164
+ foreach ( (array) $wp_registered_sidebars as $key => $value ) {
165
+ if ( sanitize_title($value['name']) == $index ) {
166
+ $index = $key;
167
+ break;
168
+ }
169
+ }
170
+ }
171
+
172
+ $sidebars_widgets = wp_get_sidebars_widgets();
173
+
174
+ /* if ($debug) {
175
+ echo '<h3> result of wp_get_sidebars_widgets()</h3>';
176
+ foreach ($sidebars_widgets as $i=>$w) {
177
+ echo '<br />'.$w['name'].' '.$w['id'];
178
+ };
179
+ }
180
+ */
181
+
182
+ /* DONT NEED TO BE ACTIVE ? if there are no active widgets */
183
+ // if ( empty($wp_registered_sidebars[$index]) ||
184
+ // !array_key_exists($index, $sidebars_widgets) ||
185
+ // !is_array($sidebars_widgets[$index])
186
+ // || empty($sidebars_widgets[$index]) ) {
187
+ // echo '<br />'.'No active widgets for '.$index;
188
+ // return false;
189
+ // }
190
+
191
+ // $sidebar = $wp_registered_sidebars[$index];
192
+ $sidebar = array('wp_inactive_widgets');
193
+ $did_one = false;
194
+
195
+ // foreach ( (array) $sidebars_widgets[$index] as $id ) { /* lifted from wordpress code, keep as similar as possible for now */
196
+
197
+ if ( !isset($wp_registered_widgets[$id]) ) continue;
198
+
199
+ $params = array_merge(
200
+ array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
201
+ (array) $wp_registered_widgets[$id]['params']
202
+ );
203
+
204
+ // Substitute HTML id and class attributes into before_widget
205
+ $classname_ = '';
206
+ foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
207
+ if ( is_string($cn) )
208
+ $classname_ .= '_' . $cn;
209
+ elseif ( is_object($cn) )
210
+ $classname_ .= '_' . get_class($cn);
211
+ }
212
+ $classname_ = ltrim($classname_, '_');
213
+ if (!empty($params[0]['before_widget']))
214
+ $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
215
+ else $params[0]['before_widget'] = '';
216
+ if (empty($params[0]['after_widget'])) $params[0]['after_widget'] = '';
217
+
218
+ $params = apply_filters( 'dynamic_sidebar_params', $params );
219
+
220
+ if (!$title) { /* amr switch off the title html, still need to get rid of title separately */
221
+ $params[0]['before_title'] = '<span style="display: none">';
222
+ $params[0]['after_title'] = '</span>';
223
+ }
224
+ else {
225
+ $params[0]['before_title'] = '<h2>';
226
+ $params[0]['after_title'] = '</h2>';
227
+ }
228
+
229
+ $callback = $wp_registered_widgets[$id]['callback'];
230
+ if ( is_callable($callback) ) {
231
+ call_user_func_array($callback, $params);
232
+ $did_one = true;
233
+ }
234
+ // }
235
+ return $did_one;
236
+ }
237
+ /* -------------------------------------------------------------------------------------------------------------*/
238
+ function amr_reg_sidebar() {
239
+ if ( function_exists('register_sidebar') )
240
+ register_sidebar(array('name'=>'Shortcodes',
241
+ 'id' => 'Shortcodes',
242
+ 'before_widget' => '<div id="%1$s" class="widget %2$s clearfix">',
243
+ 'after_widget' => '</div>',
244
+ 'before_title' => '<h2 class="widgettitle">',
245
+ 'after_title' => '</h2>' ));
246
+ }
247
+ /*-----------------------------------*/
248
+ function amr_check_if_widget_debug() {
249
+ // only do these debug if we are logged in and are the administrator
250
+
251
+ if ((!is_user_logged_in()) or (!current_user_can('administrator')))
252
+ return false;
253
+
254
+ if (isset($_REQUEST['do_widget_debug'])) {
255
+ echo '<br/>Note: Debugs only shown to a Logged in Administrator.<br />';
256
+ return true;
257
+ }
258
+ else
259
+ return false;
260
+ }
261
+ /*-----------------------------------*/
262
+ function amr_show_widget_debug($type='', $atts=array()) {
263
+ global $wp_registered_sidebars, $wp_registered_widgets,$_wp_sidebars_widgets;
264
+ // only do these debug if we are logged in and are the administrator
265
+
266
+ $debug = amr_check_if_widget_debug();
267
+
268
+ if ($type=='empty') {
269
+ if (current_user_can('administrator')) echo '<br />You are admin: <a href="'.add_query_arg('do_widget_debug','1').'">Try debug</a></b></p>';
270
+
271
+ if ($debug) {
272
+
273
+ echo '<h2>As a last resort, dump the wp variables </h2>';
274
+ $sidebars_widgets = wp_get_sidebars_widgets();
275
+ echo '<h3> result of wp_get_sidebars_widgets()</h3>';
276
+ foreach ($sidebars_widgets as $i=>$w) {
277
+ echo '<br/>'.$i; var_dump($w);
278
+ };
279
+
280
+ echo '<h3>$_wp_sidebars_widgets:</h3>';
281
+ var_dump($_wp_sidebars_widgets);
282
+ echo '<br /><h3>$wp_registered_widgets:</h3>';
283
+ var_dump($wp_registered_widgets);
284
+ }
285
+ }
286
+
287
+ if (($type=='which one') and ($debug)) {
288
+ echo '<h3>DEBUG on: Please scroll down till you find the shortcodes sidebar.</h3>';
289
+ echo '<br />Attributes entered:<br />';
290
+ var_dump($atts);
291
+ echo '<br />Shortcode sidebar and widgets<br />';
292
+ foreach ($_wp_sidebars_widgets as $i=> $w) {
293
+ if (($i == "Shortcodes")) {
294
+ echo 'Sidebar:&nbsp;<b>'.$i.': '.amr_get_sidebar_name($i).'</b><br />';
295
+ if (is_array($w)) {
296
+ sort ($w);
297
+ foreach ($w as $i2=> $w2) {
298
+ echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$w2.' <br />';
299
+ };
300
+ }
301
+ echo '<br />';
302
+ }
303
+ };
304
+ }
305
+ }
306
+ /*-----------------------------------*/
307
+ include ('amr-admin-form-html.php');
308
+ if (is_admin() ) $amr_saw_plugin_admin = new amr_saw_plugin_admin();
309
+ add_action('admin_init', 'amr_reg_sidebar');
310
+
311
+ add_shortcode('do_widget', 'do_widget');
312
+
313
+
314
+ require_once(ABSPATH . 'wp-includes/widgets.php');
315
+
316
+ ?>
readme.txt CHANGED
@@ -1,8 +1,8 @@
1
  === amr shortcode any widget ===
2
  Contributors: anmari
3
  Tags: shortcode, widget, page, templates, page template
4
- Tested up to: 3.8
5
- Version: 1.6
6
  Stable tag: trunk
7
 
8
  == Description ==
@@ -17,8 +17,9 @@ This simple 'utility' plugin allows one to have any widget used in a page shortc
17
  [do_widget widgetname] eg: [do_widget calendar]
18
  [do_widget "widget name"]. eg: [do_widget "tag cloud"]
19
  [do_widget id=widgetid] in a page or post
20
- 7. If the plugin cannot work out what you want, it will show a debug prompt. Click on the link 'Try debug'.
21
- It will produce a bunch of info. Look for the id of your widget in the shortcodes sidebar (you may have to scroll through a lot of debug nfo). Try with the id. Sometimes the widget name that wordpress calls it internally is not the same as what you see on the screen and you will need the 'debug' to find the id.
 
22
 
23
  Plugin has been tested with most standard widgets (rss feeds, tag cloud, pages, meta, search, and of course my own plugins widgets - upcoming events list, calendar and user lists.
24
 
@@ -30,6 +31,10 @@ If you liked this plugin, you might also like my other plugins:
30
 
31
 
32
  == Changelog ==
 
 
 
 
33
  = Version 1.6 =
34
  * Add: added a settings page to help people out (not really settings)
35
  * Fix: changed a clashing function name
1
  === amr shortcode any widget ===
2
  Contributors: anmari
3
  Tags: shortcode, widget, page, templates, page template
4
+ Tested up to: 3.8.1
5
+ Version: 1.7
6
  Stable tag: trunk
7
 
8
  == Description ==
17
  [do_widget widgetname] eg: [do_widget calendar]
18
  [do_widget "widget name"]. eg: [do_widget "tag cloud"]
19
  [do_widget id=widgetid] in a page or post
20
+ 7. If the plugin cannot work out what you want and you are logged in as an adminsitrator, it will show a debug prompt to you only.
21
+ Click on the link 'Try debug'.
22
+ It will produce a bunch of info. Look for the id of your widget in the shortcodes sidebar (you may have to scroll through a lot of debug info). Try wusing the widget id. Sometimes the widget name that wordpress calls it internally is not the same as what you see on the screen and you will need the 'debug' to find the id.
23
 
24
  Plugin has been tested with most standard widgets (rss feeds, tag cloud, pages, meta, search, and of course my own plugins widgets - upcoming events list, calendar and user lists.
25
 
31
 
32
 
33
  == Changelog ==
34
+ = Version 1.7 =
35
+ * Change: Changed so that debugs and debug prompt will only show to a logged in administrator.
36
+
37
+
38
  = Version 1.6 =
39
  * Add: added a settings page to help people out (not really settings)
40
  * Fix: changed a clashing function name