Widget Logic - Version 0.3

Version Description

Download this release

Release Info

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

Code changes from version 0.2 to 0.3

Files changed (3) hide show
  1. readme.txt +28 -5
  2. screenshot-2.png +0 -0
  3. widget_logic.php +40 -15
readme.txt CHANGED
@@ -3,14 +3,16 @@ 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
 
@@ -33,6 +35,7 @@ I've tested it looks OK on Safari, Firefox and even PC IE6. But let me know what
33
  == Screenshots ==
34
 
35
  1. The 'Widget logic' field at work in a widget I use.
 
36
 
37
  == Writing Logic Code ==
38
 
@@ -45,6 +48,26 @@ Examples:
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'.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  Tags: widget, admin, conditional tags
4
  Requires at least: 2.1
5
  Tested up to: 2.3.2
6
+ Stable tag: 0.3
7
 
8
+ Widget Logic lets you add 'conditional tags' logic from the usual widget admin interface. It optionally adds a 'widget_content' filter.
9
 
10
  == Description ==
11
+ This plugin gives every widget (even widgets lacking controls) an extra control called "Widget 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
+ There is also an option to add a wordpress 'widget_content' filter for you to tweak standard widgets to suit your theme.
16
 
17
  == Installation ==
18
 
35
  == Screenshots ==
36
 
37
  1. The 'Widget logic' field at work in a widget I use.
38
+ 2. The 'widget_content' filter option is at the foot of the widget admin page. (Off by default.)
39
 
40
  == Writing Logic Code ==
41
 
48
  * is\_home()
49
  * is\_category(5)
50
  * is\_home() || is\_category(5)
51
+ * $x=(1==1)?true:false; return ( !is_home && $x);
52
 
53
  Note the use of ';' where there is an explicit 'return'.
54
+
55
+ == The 'widget_content' filter ==
56
+
57
+ To filter widget contents use:
58
+
59
+ `add_filter('widget_content', 'your_filter_function' , [priority] ,2);`
60
+
61
+ your function can take 2 parameters (hence that final 2) like this:
62
+
63
+ `function your_filter_function($content='', $widget_id='')`
64
+
65
+ The second parameter (widget_id) allows you to target specific widgets. As an example here is a function I use to render all widget titles with the excellent ttftext plugin:
66
+
67
+ `function ttftext_widget_tite($content='', $widget_id='')
68
+ { preg_match("/<h2[^>]*>([^<]+)/",$content, $matches))
69
+ $heading=$matches[1];
70
+ $insert_img=the_ttftext( $heading, false );
71
+ $content=preg_replace("/(<h2[^>]*>)[^<]+/","$1$insert_img",$content,1);
72
+ return $content;
73
+ }`
screenshot-2.png ADDED
Binary file
widget_logic.php CHANGED
@@ -4,7 +4,7 @@ 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
 
@@ -32,20 +32,41 @@ function widget_logic_add_control()
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
 
@@ -69,16 +90,20 @@ function widget_logic_redirected_callback()
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
 
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.3
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
32
 
33
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
34
 
35
+ ?><script>
36
+ function insert_control(id,value)
37
+ { nc = document.createElement("p");
38
+ nc.innerHTML="<label for='" + id + "-widget_logic'>Widget logic <input type='text' name='" + id + "-widget_logic' id='" + id + "-widget_logic' value='" + value + "' /></label>";
39
+ document.getElementById(id + "control").getElementsByTagName("div").item(0).appendChild(nc);
40
+ }
41
+ <?
42
  foreach ( $wp_registered_widget_controls as $name => $widget )
43
  { $id=$widget['id'];
44
+
45
  if (isset($_POST[$id.'-widget_logic']))
46
  { $wl_options[$id]=$_POST[$id.'-widget_logic'];
47
  update_option('widget_logic', $wl_options);
48
  }
49
+ echo "\n\t\tinsert_control('".$id."', '".htmlspecialchars(stripslashes($wl_options[$id]),ENT_QUOTES)."');";
50
+ }
51
+ echo "\n\t</script>\n\t";
52
+
53
+
54
+ if ( isset($_POST['widget_logic-options-submit']) )
55
+ { $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
56
+ update_option('widget_logic', $wl_options);
57
  }
58
+ ?><div class="wrap">
59
+ <form method="POST">
60
+ <h2>Widget Logic options</h2>
61
+ <p style="line-height: 30px;">
62
+ <label for="widget_logic-options-filter">Use 'widget_content' filter?
63
+ <input id="widget_logic-options-filter" name="widget_logic-options-filter" type="checkbox" value="checked" class="checkbox" <? echo $wl_options['widget_logic-options-filter'] ?> /></label>
64
+ <span class="submit"><input type="submit" name="widget_logic-options-submit" id="widget_logic-options-submit" value="Save" /></span></p>
65
+ </form>
66
+ </div>
67
+ <?php
68
+
69
+
70
  }
71
 
72
 
90
  $callback=$wp_registered_widgets[$id]['callback_redirect']; // find the real callback
91
 
92
  $wl_options = get_option('widget_logic'); // do we want the widget?
93
+ $wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
94
  $wl_value=(stristr($wl_value, "return"))?$wl_value:"return ".$wl_value.";";
 
95
 
96
+ $wl_value=(eval($wl_value) && is_callable($callback));
97
  if ( $wl_value )
98
+ { if ($wl_options['widget_logic-options-filter']!='checked')
99
+ call_user_func_array($callback, $params); // if so callback with original params!
100
+ else
101
+ { ob_start();
102
+ call_user_func_array($callback, $params); // if so callback with original params!
103
+ $widget_content = ob_get_contents();
104
+ ob_end_clean();
105
+ echo apply_filters( 'widget_content', $widget_content, $id);
106
+ }
107
  }
108
  }
109