amr shortcode any widget - Version 1.0

Version Description

  • Launch of the plugin

=

Download this release

Release Info

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

Version 1.0

amr_shortcode_any_widget.php ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
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
+ */
11
+
12
+
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';}
44
+ return (false);
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 */
94
+ global $wp_registered_sidebars;
95
+ foreach ($wp_registered_sidebars as $i => $a) {
96
+ if ((isset ($a['name'])) and ( $a['name'] === $name)) return ($i);
97
+ }
98
+ return (false);
99
+ }
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";
106
+ } else {
107
+ $index = sanitize_title($index);
108
+ foreach ( (array) $wp_registered_sidebars as $key => $value ) {
109
+ if ( sanitize_title($value['name']) == $index ) {
110
+ $index = $key;
111
+ break;
112
+ }
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 */
129
+
130
+ if ( !isset($wp_registered_widgets[$id]) ) continue;
131
+
132
+ $params = array_merge(
133
+ array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
134
+ (array) $wp_registered_widgets[$id]['params']
135
+ );
136
+
137
+ // Substitute HTML id and class attributes into before_widget
138
+ $classname_ = '';
139
+ foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
140
+ if ( is_string($cn) )
141
+ $classname_ .= '_' . $cn;
142
+ elseif ( is_object($cn) )
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
+
150
+ if (!$title) { /* amr switch off the title html, still need to get rid of title separately */
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) ) {
157
+ call_user_func_array($callback, $params);
158
+ $did_one = true;
159
+ }
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
+ ?>
readme.txt ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
34
+ 1. Demonstration of two widgets being used via the do_widget short code.
35
+ 2. The Page or post with the do_widget shortcodes
36
+ 3. The shortcode sidebar. The widget's user interface (UI) is used to provide a UI for the do_widget shortcode.
37
+
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
screenshot-3.png ADDED
Binary file