amr shortcode any widget - Version 1.3

Version Description

Download this release

Release Info

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

Code changes from version 1.0 to 1.3

Files changed (2) hide show
  1. amr_shortcode_any_widget.php +151 -46
  2. readme.txt +25 -17
amr_shortcode_any_widget.php CHANGED
@@ -2,9 +2,9 @@
2
  /*
3
  Plugin Name: amr shortcode any widget
4
  Plugin URI: http://webdesign.anmari.com/shortcode-any-widget/
5
- Description: Allows inclusion of any widget within a page for any theme. [do_widget widgetname ] or [do_widget "widget name" ]
6
- Author: Anna-marie Redpath
7
- Version: 1.0 alpha
8
  Author URI: http://webdesign.anmari.com
9
 
10
  */
@@ -13,31 +13,66 @@ Author URI: http://webdesign.anmari.com
13
  /*-----------------------------------*/
14
  function do_widget($atts) {
15
 
16
- global $wp_registered_widgets,$_wp_sidebars_widgets;
 
17
 
18
  /* check if the widget is in the shortcode x sidebar if not , just use generic,
19
  if it is in, then get the instance data and use that */
20
- if (isset($_REQUEST['debug'])) $debug=true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  extract(shortcode_atts(array(
23
- 'sidebar' => 'Shortcode',
24
  'id' => '',
25
  'title' => 'true' /* do the default title unless they ask us not to - use string here not boolean */
26
- ), $atts));
 
 
27
 
28
  /* the widget need not be specified, [do_widget widgetname] is adequate */
29
  if (!empty($atts[0])) {
 
 
 
 
 
30
  $widget = $atts[0];
 
31
  foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
32
- if ($w['name'] === $widget) $widget_ids[] = $i;
 
33
  }
 
 
34
  }
35
- else { /* check for id */
36
  if (!empty($id)) { /* if a specific id has been specified */
 
37
  foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
38
- if ($debug) { echo '<br> id = '.$id.' - '.$w['id'] ;}
39
- if ($w['id'] === $id) $widget_ids[] = $i;
40
  }
 
41
  }
42
  else {
43
  if ($debug) { echo 'No valid widget name or id given';}
@@ -45,49 +80,93 @@ if (isset($_REQUEST['debug'])) $debug=true;
45
 
46
  }
47
  }
 
48
  if (empty ($widget_ids)) {
49
- if ($debug) { echo '<h2>Widget not found in widget list </h2>'; print_r($wp_registered_widgets);}
50
- return ;
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
- if ($title=='false') $title = false;
 
 
 
53
  else $title = true;
54
 
55
- $sidebarid = get_sidebar_id ($sidebar); /* get the official sidebar id - will take the first one */
56
 
57
- if ($debug) { echo '<hr><h2>Looking for widget:'.$widget.' </h2>'; echo 'found instances:'; print_r ($widget_ids); }
 
 
 
 
 
58
  $content = '';
59
  /* if the widget is in our chosen sidebar, then use the otions stored for that */
60
- if (isset($_wp_sidebars_widgets) ) {
61
- if ((isset ($_wp_sidebars_widgets[$sidebarid])) and (!empty ($_wp_sidebars_widgets[$sidebarid]))) {
62
- if ($debug) {
63
- echo '<h2>Widget ids in sidebar in sidebar '.$sidebar.' with id:'.$sidebarid .'</h2>';
64
- print_r($_wp_sidebars_widgets[$sidebarid]);
65
- }
 
 
 
66
  /* get the intersect of the 2 widget setups so we just get the widget we want */
 
67
  $wid = array_intersect ($_wp_sidebars_widgets[$sidebarid], $widget_ids );
68
- if ($debug) { echo '<br />Chosen Widget ids in Chosen sidebar';print_r($wid);}
 
 
 
 
69
  }
70
  else { /* the sidebar is not defined */
71
- if ($debug) {echo '<br />Sidebar '.$sidebar.' empty or not defined.'; }
72
- }
73
- }
74
- else { if ($debug) {echo '<br />No widgets defined'; }
75
- return (false);
76
  }
 
 
77
  if (empty ($wid)) {
78
- unset($sidebar); unset($sidebarid);
79
  if ($debug) { echo '<h2>No Widget ids in sidebar '.$sidebarid.' with name '.$sidebar.' Try defaults </h2>';}
 
 
80
  }
81
  else
82
  /* There may only be one but if we have two in our chosen widget then it will do both */
 
83
  foreach ($wid as $i=>$widget_instance) {
84
  ob_start(); /* catch the echo output, so we can control where it appears in the text */
85
  shortcode_sidebar($widget_instance, $sidebar, $title);
86
  $output .= ob_get_clean();
87
  }
 
 
 
88
  return ($output);
89
  }
90
-
 
 
 
 
 
 
 
 
 
 
 
91
  /*-----------------------------------*/
92
  function get_sidebar_id ($name) { /* dont need anymore ? or at least temporarily */
93
  /* walk through the registered sidebars with a name and find the id - will be something like sidebar-integer. take the first one */
@@ -100,6 +179,9 @@ global $wp_registered_sidebars;
100
  /* -------------------------------------------------------------------------*/
101
  function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically the wordpress code, slightly modified */
102
  global $wp_registered_sidebars, $wp_registered_widgets;
 
 
 
103
 
104
  if ( is_int($index) ) {
105
  $index = "sidebar-$index";
@@ -113,16 +195,27 @@ function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically t
113
  }
114
  }
115
 
116
- $sidebars_widgets = wp_get_sidebars_widgets();
117
- /* if there are no active widgets */
118
- if ( empty($wp_registered_sidebars[$index]) ||
119
- !array_key_exists($index, $sidebars_widgets) ||
120
- !is_array($sidebars_widgets[$index])
121
- || empty($sidebars_widgets[$index]) )
122
- return false;
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- $sidebar = $wp_registered_sidebars[$index];
125
- // $sidebar = array('wp_inactive_widgets');
126
  $did_one = false;
127
 
128
  // foreach ( (array) $sidebars_widgets[$index] as $id ) { /* lifted from wordpress code, keep as similar as possible for now */
@@ -143,7 +236,10 @@ function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically t
143
  $classname_ .= '_' . get_class($cn);
144
  }
145
  $classname_ = ltrim($classname_, '_');
146
- $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
 
 
 
147
 
148
  $params = apply_filters( 'dynamic_sidebar_params', $params );
149
 
@@ -151,6 +247,10 @@ function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically t
151
  $params[0]['before_title'] = '<span style="display: none">';
152
  $params[0]['after_title'] = '</span>';
153
  }
 
 
 
 
154
 
155
  $callback = $wp_registered_widgets[$id]['callback'];
156
  if ( is_callable($callback) ) {
@@ -160,16 +260,21 @@ function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically t
160
  // }
161
  return $did_one;
162
  }
163
-
164
  /* -------------------------------------------------------------------------------------------------------------*/
165
- add_shortcode('do_widget', 'do_widget');
166
- /* Create a sidebar that will not appear in any theme, but can be used to customise widget settings if default settings not suitable */
167
  if ( function_exists('register_sidebar') )
168
- register_sidebar(array('name'=>'Shortcode',
169
- 'id' => 'sidebar-shortcode',
170
  'before_widget' => '<div id="%1$s" class="widget %2$s clearfix">',
171
  'after_widget' => '</div>',
172
  'before_title' => '<h2 class="widgettitle">',
173
- 'after_title' => '</h2>' ));
 
 
 
 
 
 
 
174
 
175
  ?>
2
  /*
3
  Plugin Name: amr shortcode any widget
4
  Plugin URI: http://webdesign.anmari.com/shortcode-any-widget/
5
+ Description: Allows inclusion of any widget within a page for any theme. [do_widget widgetname ] or [do_widget "widget name" ]. Warning if upgraded from 1.1 t- Don't panic, please see changelog about the shortcodes sidebar. Add ?do_widget_debug to see list of widget ids that can be used.
6
+ Author: anmari
7
+ Version: 1.3
8
  Author URI: http://webdesign.anmari.com
9
 
10
  */
13
  /*-----------------------------------*/
14
  function do_widget($atts) {
15
 
16
+ global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars;
17
+
18
 
19
  /* check if the widget is in the shortcode x sidebar if not , just use generic,
20
  if it is in, then get the instance data and use that */
21
+ if (isset($_REQUEST['do_widget_debug'])) $debug=true;
22
+ else $debug = false;
23
+
24
+ if (isset($_wp_sidebars_widgets) ) {
25
+ if ($debug) {
26
+ echo '<br />Attributes entered:<br />';
27
+ var_dump($atts);
28
+ echo '<br />Available sidebars and widgets<br />';
29
+ foreach ($_wp_sidebars_widgets as $i=> $w) {
30
+ echo 'Sidebar:&nbsp;<b>'.$i.': '.get_sidebar_name($i).'</b><br />';
31
+ if (is_array($w)) {
32
+ sort ($w);
33
+ foreach ($w as $i2=> $w2) {
34
+ echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$w2.' <br />';
35
+ };
36
+ }
37
+ echo '<br />';
38
+ };
39
+ }
40
+ }
41
+ else { if ($debug) {echo '<br />No widgets defined'; }
42
+ return (false);
43
+ }
44
 
45
  extract(shortcode_atts(array(
46
+ 'sidebar' => 'Shortcodes',
47
  'id' => '',
48
  'title' => 'true' /* do the default title unless they ask us not to - use string here not boolean */
49
+ ), $atts));
50
+
51
+
52
 
53
  /* the widget need not be specified, [do_widget widgetname] is adequate */
54
  if (!empty($atts[0])) {
55
+ if ($debug) {
56
+ echo 'We have a name';
57
+ print_r($atts[0]);
58
+ //var_dump($wp_registered_widgets);
59
+ }
60
  $widget = $atts[0];
61
+
62
  foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
63
+ if (strtolower($w['name']) === strtolower($widget)) $widget_ids[] = $i;
64
+ if ($debug) {echo '<br /> Check: '.$w['name'];}
65
  }
66
+
67
+
68
  }
69
+ else { /* check for id if we do not have a name */
70
  if (!empty($id)) { /* if a specific id has been specified */
71
+
72
  foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */
73
+ if ($w['id'] === $id) $widget_ids[] = $id;
 
74
  }
75
+ if ($debug) {echo '<h2>We have an id: '.$id.'</h2>'; var_dump($widget_ids);}
76
  }
77
  else {
78
  if ($debug) { echo 'No valid widget name or id given';}
80
 
81
  }
82
  }
83
+
84
  if (empty ($widget_ids)) {
85
+
86
+ echo '<p><b>Widget not found in widget list.'
87
+ .' <a href="'.add_query_arg('do_widget_debug','1').'">Try debug</a></b></p>';
88
+ if ($debug) {
89
+ echo '<h2>As a last resort, dump the wp variables </h2>';
90
+ $sidebars_widgets = wp_get_sidebars_widgets();
91
+ echo '<h3> result of wp_get_sidebars_widgets()</h3>';
92
+ foreach ($sidebars_widgets as $i=>$w) {
93
+ echo '<br/>'.$i; var_dump($w);
94
+ };
95
+
96
+ echo '<h3>$_wp_sidebars_widgets:</h3>';
97
+ var_dump($_wp_sidebars_widgets);
98
+ echo '<br /><h3>$wp_registered_widgets:</h3>';
99
+ var_dump($wp_registered_widgets);
100
  }
101
+ return (false) ;
102
+ }
103
+
104
+ if ($title == 'false') $title = false; /* If ask not to display title, then do not */
105
  else $title = true;
106
 
107
+ if (!($sidebarid = get_sidebar_id ($sidebar))) $sidebarid=$sidebar; /* get the official sidebar id - will take the first one */
108
 
109
+ if ($debug) {
110
+ echo '<hr>Looking for widget with name:'.$widget.' or id='.$id.' Found instances:'.' <br />';
111
+ foreach ($widget_ids as $i=> $w) {
112
+ echo $w.'<br />';
113
+ };
114
+ }
115
  $content = '';
116
  /* if the widget is in our chosen sidebar, then use the otions stored for that */
117
+
118
+ if ((isset ($_wp_sidebars_widgets[$sidebarid])) and (!empty ($_wp_sidebars_widgets[$sidebarid]))) {
119
+ if ($debug) {
120
+ echo '<br />Widget ids in sidebar: "'.$sidebar.'" with id: '.$sidebarid .'<br />';
121
+ sort ($_wp_sidebars_widgets[$sidebarid]);
122
+ foreach ($_wp_sidebars_widgets[$sidebarid] as $i=> $w) {
123
+ echo $i.' '.$w.'<br />';
124
+ };
125
+ }
126
  /* get the intersect of the 2 widget setups so we just get the widget we want */
127
+
128
  $wid = array_intersect ($_wp_sidebars_widgets[$sidebarid], $widget_ids );
129
+ if ($debug) { echo '<br />Will use widget ids'.'<br />';
130
+ foreach ($widget_ids as $i=> $w) {
131
+ echo '&nbsp;&nbsp;&nbsp;'.$w.'<br />';
132
+ };
133
+ }
134
  }
135
  else { /* the sidebar is not defined */
136
+ if ($debug) {echo '<br />Sidebar '.$sidebar.'with sidebarid '.$sidebarid.' empty or not defined.'; }
 
 
 
 
137
  }
138
+
139
+
140
  if (empty ($wid)) {
 
141
  if ($debug) { echo '<h2>No Widget ids in sidebar '.$sidebarid.' with name '.$sidebar.' Try defaults </h2>';}
142
+ unset($sidebar); unset($sidebarid);
143
+
144
  }
145
  else
146
  /* There may only be one but if we have two in our chosen widget then it will do both */
147
+ $output = '';
148
  foreach ($wid as $i=>$widget_instance) {
149
  ob_start(); /* catch the echo output, so we can control where it appears in the text */
150
  shortcode_sidebar($widget_instance, $sidebar, $title);
151
  $output .= ob_get_clean();
152
  }
153
+
154
+
155
+
156
  return ($output);
157
  }
158
+ /*-----------------------------------*/
159
+ function get_sidebar_name ($id) { /* dont need anymore ? or at least temporarily */
160
+ /* walk through the registered sidebars with a name and find the id - will be something like sidebar-integer. take the first one */
161
+ global $wp_registered_sidebars;
162
+ foreach ($wp_registered_sidebars as $i => $a) {
163
+ if ((isset ($a['id'])) and ( $a['id'] === $id)) {
164
+ if (isset($a['name'])) return ($a['name']);
165
+ else return ($id);
166
+ }
167
+ }
168
+ return (false);
169
+ }
170
  /*-----------------------------------*/
171
  function get_sidebar_id ($name) { /* dont need anymore ? or at least temporarily */
172
  /* walk through the registered sidebars with a name and find the id - will be something like sidebar-integer. take the first one */
179
  /* -------------------------------------------------------------------------*/
180
  function shortcode_sidebar( $id, $index=1, $title=true) { /* This is basically the wordpress code, slightly modified */
181
  global $wp_registered_sidebars, $wp_registered_widgets;
182
+
183
+ if (isset($_REQUEST['do_widget_debug'])) $debug=true;
184
+ else $debug = false;
185
 
186
  if ( is_int($index) ) {
187
  $index = "sidebar-$index";
195
  }
196
  }
197
 
198
+ $sidebars_widgets = wp_get_sidebars_widgets();
199
+
200
+ if ($debug) {
201
+ echo '<h3> result of wp_get_sidebars_widgets()</h3>';
202
+ foreach ($sidebars_widgets as $i=>$w) {
203
+ echo '<br />'.$w['name'].' '.$w['id'];
204
+ };
205
+ }
206
+
207
+
208
+ /* DONT NEED TO BE ACTIVE ? if there are no active widgets */
209
+ // if ( empty($wp_registered_sidebars[$index]) ||
210
+ // !array_key_exists($index, $sidebars_widgets) ||
211
+ // !is_array($sidebars_widgets[$index])
212
+ // || empty($sidebars_widgets[$index]) ) {
213
+ // echo '<br />'.'No active widgets for '.$index;
214
+ // return false;
215
+ // }
216
 
217
+ // $sidebar = $wp_registered_sidebars[$index];
218
+ $sidebar = array('wp_inactive_widgets');
219
  $did_one = false;
220
 
221
  // foreach ( (array) $sidebars_widgets[$index] as $id ) { /* lifted from wordpress code, keep as similar as possible for now */
236
  $classname_ .= '_' . get_class($cn);
237
  }
238
  $classname_ = ltrim($classname_, '_');
239
+ if (!empty($params[0]['before_widget']))
240
+ $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
241
+ else $params[0]['before_widget'] = '';
242
+ if (empty($params[0]['after_widget'])) $params[0]['after_widget'] = '';
243
 
244
  $params = apply_filters( 'dynamic_sidebar_params', $params );
245
 
247
  $params[0]['before_title'] = '<span style="display: none">';
248
  $params[0]['after_title'] = '</span>';
249
  }
250
+ else {
251
+ $params[0]['before_title'] = '<h2>';
252
+ $params[0]['after_title'] = '</h2>';
253
+ }
254
 
255
  $callback = $wp_registered_widgets[$id]['callback'];
256
  if ( is_callable($callback) ) {
260
  // }
261
  return $did_one;
262
  }
 
263
  /* -------------------------------------------------------------------------------------------------------------*/
264
+ function amr_reg_sidebar() {
 
265
  if ( function_exists('register_sidebar') )
266
+ register_sidebar(array('name'=>'Shortcodes',
267
+ 'id' => 'Shortcodes',
268
  'before_widget' => '<div id="%1$s" class="widget %2$s clearfix">',
269
  'after_widget' => '</div>',
270
  'before_title' => '<h2 class="widgettitle">',
271
+ 'after_title' => '</h2>' ));
272
+ }
273
+ /* -------------------------------------------------------------------------------------------------------------*/
274
+
275
+ add_action('admin_init', 'amr_reg_sidebar');
276
+ add_shortcode('do_widget', 'do_widget');
277
+
278
+ require_once(ABSPATH . 'wp-includes/widgets.php');
279
 
280
  ?>
readme.txt CHANGED
@@ -1,33 +1,41 @@
1
- === AmR shortcode any widget ===
2
- Contributors: Anmari
3
- Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=anmari%40anmari%2ecom&item_name=Shortcode widget Plugin Support&no_shipping=1&no_note=1&cn=Optional%20Notes&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8">Donate</a>
4
  Tags: shortcode widget page
5
- Tested up to: 2.8.4
6
- Version: 1.0 alpha
7
  Stable tag: trunk
8
 
9
  == Description ==
10
- This simple 'utility' plugin allows one to have any widget used in a page shortcode in any theme - no need to use the hybrid theme or create a special template.
11
 
12
- If you found the plugin useful, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=anmari%40anmari%2ecom&item_name=Shortcode widget Support&no_shipping=1&no_note=1&cn=Optional%20Notes&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8">Donate</a>
13
 
14
  == Changelog ==
15
- = 1.0 =
16
- * Launch of the plugin
 
 
 
 
17
 
 
 
 
 
 
 
 
18
 
19
  == Installation ==
20
 
21
- 1. In the wordpress admin plugin section of your site, click "Add New" or download and Unzip the file into your wordpress plugins folder.
22
- 2. Activate the plugin through the 'Plugins' menu in WordPress
23
- 3. Activate the widget plugin that you want to use.
24
- 4. Go to the widgets menu and drag an instance of the widget to the shortcodes sidebar, and set the widget options.
25
- 5. Create or edit the page or post in which you wish to use the widget, enter [do_widget widgetname] within the text in the page and save.
26
 
27
- Other variations:
28
- If you use a widget more than once for different shortcodes, you can use the widget id to isolate which widget instance (and of course assocaied settings to use).
29
-
30
 
 
 
31
 
32
  == Screenshots ==
33
 
1
+ === amr shortcode any widget ===
2
+ Contributors: anmari
 
3
  Tags: shortcode widget page
4
+ Tested up to: 3.2.1
5
+ Version: 1.3
6
  Stable tag: trunk
7
 
8
  == Description ==
9
+ This simple 'utility' plugin allows one to have any widget used in a page shortcode in any theme - no need to use the hybrid theme or create a special template. For more details see [anmari.com](http://webdesign.anmari.com/category/plugins/shortcode-any-widget/)
10
 
 
11
 
12
  == Changelog ==
13
+ = Version 1.3 =
14
+ * Added debug link and retested. Added readme.
15
+
16
+ = 1.12=
17
+ * Changed dummy shortcode sidebar so it appears after the theme sidebars to avoid taking over their widgets (this happened in numbered sidebars) PLEASE note if you have upgraded, you may appear to have "lost" your widgets due to this sidebar change. You have not - they will be in your "inactive widgets" - please drag them to the new sidebar. You may also find that you have 2 versions of the plugin, with slightly different folder names. This will allow you to go back to the previous one to check what you had there if you need to. Deactivate the old one and activate the new one. Move your widgets back in. Test then Delete the old one. In one theme it also appeared that somehow due to this change, one of the sidebar widgets "moved" down to another sidebar. I think that this may have had something to do with the fact that sidebars can be numbered or named, so please check all your sidebars before continuing. As always make sure thath you know how to restore from a backup before doing any upgrades of any kind.
18
+ * Tested in 2.9.2, can still use either the widget name "Categories" or id=categories-6. Note widget must have been created in the dummy shortcode sidebar.
19
 
20
+ = 1.1 =
21
+
22
+ * Allow for lowercase use of widget names for the those challenged by attention to detail!
23
+ * Cleaned up debug mode to make it easier for people to identify which instance of a widget they are using. Add &do_widget_debug to url string.
24
+
25
+ = 1.0 =
26
+ * Launch of the plugin
27
 
28
  == Installation ==
29
 
30
+ 0. Activate plugin
31
+ 1. Drag widgets to shortcodes sidebar. Save.
32
+ 2. Add [do_widget id=widgetid] in a page or post
33
+ 3. If it fails, click on the debug prompt and look for the id of your widget, use that.
 
34
 
35
+ Or can use [do_widget widgetname] within the text in the page and save. If the widget name has a space in it, use [do_widget "widget name"].
 
 
36
 
37
+ If you use a widget more than once for different shortcodes, you can use the widget id to isolate which widget instance (and of course associated settings to use). ie: [do_widget id=categories-6]
38
+
39
 
40
  == Screenshots ==
41