Widget Logic - Version 0.2

Version Description

Download this release

Release Info

Developer alanft
Plugin Icon 128x128 Widget Logic
Version 0.2
Comparing to
See all releases

Version 0.2

Files changed (3) hide show
  1. readme.txt +50 -0
  2. screenshot-1.png +0 -0
  3. widget_logic.php +86 -0
readme.txt ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Widget Logic ===
2
+ Contributors: alanft
3
+ Tags: widget, admin, conditional tags
4
+ Requires at least: 2.1
5
+ Tested up to: 2.3.2
6
+ Stable tag: 0.2
7
+
8
+ Widget Logic lets you add 'conditional tags' logic from the usual widget admin interface. It also adds a 'widget_content' filter.
9
+
10
+ == Description ==
11
+ This plugin gives every widget (even widgets lacking controls) an extra control called "Widgets logic".
12
+
13
+ This text field allows you to specify any wp conditional tags logic to set when the widget appears. Use any standard [Conditional Tags](http://codex.wordpress.org/Conditional_Tags) and even combine them.
14
+
15
+ == Installation ==
16
+
17
+ 1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
18
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
19
+ 3. That's it. All the configuring is in the usual widget admin interface.
20
+
21
+ == Frequently Asked Questions ==
22
+
23
+ = I get no sidebar at all when the plugin is active =
24
+
25
+ The logic text on one of your widgets may be invalid PHP.
26
+
27
+ = The 'Widget Logic' field is missing or appears elsewhere =
28
+
29
+ The plugin uses a tiny bit of javascript to get the field into position on the admin page. As the widget admin uses JS, I feel this is no imposition. However it might mean that your browser dislikes the DOM tricks that the JS code adopts.
30
+
31
+ I've tested it looks OK on Safari, Firefox and even PC IE6. But let me know what browser you are using and I'll see what I can do.
32
+
33
+ == Screenshots ==
34
+
35
+ 1. The 'Widget logic' field at work in a widget I use.
36
+
37
+ == Writing Logic Code ==
38
+
39
+ The text you write in the 'Widget logic' field can currently be full PHP code so long as it returns 'true' when you need the widget to appear - note that you need terminating ';'s.
40
+
41
+ If there is no 'return' in the text, there is an implicit 'return ' added on the start and a ';' on the end.
42
+
43
+ Examples:
44
+
45
+ * is\_home()
46
+ * is\_category(5)
47
+ * is\_home() || is\_category(5)
48
+ * x=(1==1)?true:false; return ( !is_home && x);
49
+
50
+ Note the use of ';' where there is an explicit 'return'.
screenshot-1.png ADDED
Binary file
widget_logic.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Widget Logic
4
+ Plugin URI: http://freakytrigger.co.uk/
5
+ Description: Allows widgets to appear as directed by WP tags is_home etc
6
+ Author: Alan Trewartha
7
+ Version: 0.2
8
+ Author URI: http://freakytrigger.co.uk/author/alan/
9
+ */
10
+
11
+
12
+
13
+ // before the page is drawn add minimal controls to ALL widgets and make control boxen deeper
14
+ // needs to happen before wp_widgets_admin_head
15
+ add_action( 'admin_head', 'widget_logic_expand_control', 5);
16
+ function widget_logic_expand_control()
17
+ { global $wp_registered_widgets, $wp_registered_widget_controls;
18
+ foreach ( $wp_registered_widgets as $name => $widget )
19
+ { if ( !isset( $wp_registered_widget_controls[$name] ) )
20
+ register_widget_control($widget['name'], 'widget_logic_empty_control', 250,61);
21
+ $wp_registered_widget_controls[$name]['height']+=40;
22
+ }
23
+
24
+ }
25
+
26
+ function widget_logic_empty_control() {}
27
+
28
+ // after the controls drawn, add INPUT by plaing with DOM
29
+ add_action('sidebar_admin_page', 'widget_logic_add_control');
30
+ function widget_logic_add_control()
31
+ { global $wp_registered_widget_controls;
32
+
33
+ if(!$wl_options = get_option('widget_logic')) $wl_options = array();
34
+
35
+ ?><script><?
36
+ foreach ( $wp_registered_widget_controls as $name => $widget )
37
+ { $id=$widget['id'];
38
+ if (isset($_POST[$id.'-widget_logic']))
39
+ { $wl_options[$id]=$_POST[$id.'-widget_logic'];
40
+ update_option('widget_logic', $wl_options);
41
+ }
42
+
43
+ ?> nc = document.createElement("p");
44
+ nc.innerHTML="<label for='<? echo $id ?>'>Widget logic <input type='text' name='<? echo $id.'-widget_logic' ?>' value='<? echo $wl_options[$id] ?>' /></label>";
45
+ document.getElementById('<? echo $id."control" ?>').getElementsByTagName("div").item(0).appendChild(nc);
46
+ <?
47
+ }
48
+ ?></script><?
49
+ }
50
+
51
+
52
+ // intercept EVERY registered widget - redirect it and put its ID on the end of the params
53
+ // perhaps there is a way to just intercept the ones that are used??
54
+ add_action('wp_head', 'widget_logic_redirect_callback');
55
+ function widget_logic_redirect_callback()
56
+ { global $wp_registered_widgets;
57
+ foreach ( $wp_registered_widgets as $id => $widget )
58
+ { array_push($wp_registered_widgets[$id]['params'],$id);
59
+ $wp_registered_widgets[$id]['callback_redirect']=$wp_registered_widgets[$id]['callback'];
60
+ $wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
61
+ }
62
+ }
63
+
64
+ // the redirection comes here
65
+ function widget_logic_redirected_callback()
66
+ { global $wp_registered_widgets;
67
+ $params=func_get_args(); // get all the passed params
68
+ $id=array_pop($params); // take off the widget ID
69
+ $callback=$wp_registered_widgets[$id]['callback_redirect']; // find the real callback
70
+
71
+ $wl_options = get_option('widget_logic'); // do we want the widget?
72
+ $wl_value=($wl_options[$id])?$wl_options[$id]:"true";
73
+ $wl_value=(stristr($wl_value, "return"))?$wl_value:"return ".$wl_value.";";
74
+ $wl_value=(eval($wl_value) && is_callable($callback));
75
+
76
+ if ( $wl_value )
77
+ { ob_start();
78
+ call_user_func_array($callback, $params); // if so callback with original params!
79
+ $widget_content = ob_get_contents();
80
+ ob_end_clean();
81
+ echo apply_filters( 'widget_content', $widget_content, $id);
82
+ }
83
+ }
84
+
85
+
86
+ ?>