Version Description
FINALLY tracked down the elusive 'wp_reset_query' option resetting bug.
Download this release
Release Info
Developer | alanft |
Plugin | Widget Logic |
Version | 0.47 |
Comparing to | |
See all releases |
Code changes from version 0.46 to 0.47
- readme.txt +25 -22
- widget_logic.php +9 -2
readme.txt
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
=== Widget Logic ===
|
2 |
Contributors: alanft
|
3 |
Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
|
4 |
-
Tags: widget, admin, conditional tags, filter
|
5 |
Requires at least: 2.5
|
6 |
-
Tested up to:
|
7 |
-
Stable tag: 0.
|
8 |
|
9 |
Widget Logic lets you control on which pages widgets appear. It uses any of WP's conditional tags. It also adds a 'widget_content' filter.
|
10 |
|
@@ -47,7 +47,7 @@ Tighten up your definitions with PHPs 'logical AND' &&, for example:
|
|
47 |
|
48 |
`is_single() && in_category('cheese')`
|
49 |
|
50 |
-
Another source of confusion is the difference between the [Main Page and the front page](http://codex.wordpress.org/Conditional_Tags#The_Main_Page)
|
51 |
|
52 |
= How do I get a widget to appear both on a category page and on single posts within that category? =
|
53 |
Again, take care with your conditional tags. There is both an `in_category` and `is_category` tag. One is used to tell if the 'current' post is IN a category, and the other is used to tell if the page showing IS for that category (same goes for tags etc). What you want is the case when:
|
@@ -55,7 +55,7 @@ Again, take care with your conditional tags. There is both an `in_category` and
|
|
55 |
`(this page IS category X) OR (this is a single post AND this post is IN category X)`
|
56 |
which in proper PHP is:
|
57 |
|
58 |
-
`is_category(X) || (is_single() && in_category(X)`
|
59 |
|
60 |
See also: 'Writing Logic Code' in the [Other Notes](../other_notes/) section.
|
61 |
|
@@ -70,13 +70,19 @@ The text in the 'Widget logic' field can be full PHP code and should return 'tru
|
|
70 |
|
71 |
If there is no 'return' in the text, an implicit 'return' is added to the start and a ';' is added on the end.
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
Examples:
|
74 |
|
75 |
-
* `is_home()` -- main blog
|
76 |
-
*
|
77 |
-
*
|
78 |
* `is_single() && in_category('baked-goods')` -- single post that's in the category with this slug
|
79 |
-
* `current_user_can('level_10')` -- admin only widget
|
80 |
* `strpos($_SERVER['HTTP_REFERER'], "google.com")!=false` -- widget to show when clicked through from a google search
|
81 |
* `is_category() && in_array($cat, get_term_children( 5, 'category'))` -- category page that's a descendent of category 5
|
82 |
* `global $post; return (in_array(77,get_post_ancestors($post)));` -- WP page that is a child of page 77
|
@@ -84,21 +90,13 @@ Examples:
|
|
84 |
|
85 |
Note the extra ';' on the end where there is an explicit 'return'.
|
86 |
|
87 |
-
= Build your own =
|
88 |
-
|
89 |
-
Try variations on the examples above. Use `!` (NOT) in front of a conditional tag to reverse the logic, eg !is_home() to show a widget on any page except the home page.
|
90 |
-
|
91 |
-
Use `||` (OR), `&&` (AND) to make more complex conditions. There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as [Test if post is in a descendent category](http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category).
|
92 |
-
|
93 |
-
Remember -- the code runs even if the widget doesn't appear. (Even if it never appears!)
|
94 |
-
|
95 |
== The 'widget_content' filter ==
|
96 |
|
97 |
-
|
98 |
|
99 |
`add_filter('widget_content', 'your_filter_function', [priority], 2);`
|
100 |
|
101 |
-
where `[priority]` is the optional priority parameter for the [add_filter](http://codex.wordpress.org/Function_Reference/add_filter) function. The filter function can take
|
102 |
|
103 |
`function your_filter_function($content='', $widget_id='')`
|
104 |
|
@@ -108,7 +106,8 @@ _Example filters_
|
|
108 |
|
109 |
I was motivated to make this filter in order to render all widget titles with the excellent [ttftitles plugin](http://templature.com/2007/10/18/ttftitles-wordpress-plugin/) like this:
|
110 |
|
111 |
-
`
|
|
|
112 |
{ preg_match("/<h2[^>]*>([^<]+)/",$content, $matches);
|
113 |
$heading=$matches[1];
|
114 |
$insert_img=the_ttftext( $heading, false );
|
@@ -116,9 +115,10 @@ I was motivated to make this filter in order to render all widget titles with th
|
|
116 |
return $content;
|
117 |
}`
|
118 |
|
119 |
-
|
120 |
|
121 |
-
`
|
|
|
122 |
{ global $wl_make_alt_ws;
|
123 |
$wl_make_alt_ws=($wl_make_alt_ws=="style_a")?"style_b":"style_a";
|
124 |
return preg_replace('/(class="widget )/', "$1 widget_${wl_make_alt_ws} ", $content);
|
@@ -127,6 +127,9 @@ I often see people asking for ways to give widgets alternating styles. This filt
|
|
127 |
|
128 |
== Changelog ==
|
129 |
|
|
|
|
|
|
|
130 |
= 0.46 =
|
131 |
Fix to work with new WP2.8 admin ajax. With bonus fixes.
|
132 |
|
1 |
=== Widget Logic ===
|
2 |
Contributors: alanft
|
3 |
Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
|
4 |
+
Tags: widget, admin, conditional tags, filter, context
|
5 |
Requires at least: 2.5
|
6 |
+
Tested up to: 3.0.1
|
7 |
+
Stable tag: 0.47
|
8 |
|
9 |
Widget Logic lets you control on which pages widgets appear. It uses any of WP's conditional tags. It also adds a 'widget_content' filter.
|
10 |
|
47 |
|
48 |
`is_single() && in_category('cheese')`
|
49 |
|
50 |
+
Another source of confusion is the difference between the [Main Page and the front page](http://codex.wordpress.org/Conditional_Tags#The_Main_Page). If you have set your front page to be a static page, use is_front_page() instead of is_page(x).
|
51 |
|
52 |
= How do I get a widget to appear both on a category page and on single posts within that category? =
|
53 |
Again, take care with your conditional tags. There is both an `in_category` and `is_category` tag. One is used to tell if the 'current' post is IN a category, and the other is used to tell if the page showing IS for that category (same goes for tags etc). What you want is the case when:
|
55 |
`(this page IS category X) OR (this is a single post AND this post is IN category X)`
|
56 |
which in proper PHP is:
|
57 |
|
58 |
+
`is_category(X) || (is_single() && in_category(X))`
|
59 |
|
60 |
See also: 'Writing Logic Code' in the [Other Notes](../other_notes/) section.
|
61 |
|
70 |
|
71 |
If there is no 'return' in the text, an implicit 'return' is added to the start and a ';' is added on the end.
|
72 |
|
73 |
+
Try variations on the examples given below. Use `!` (NOT) in front of a conditional tag to reverse the logic, eg !is_home() to show a widget on any page except the home page.
|
74 |
+
|
75 |
+
Use `||` (OR), `&&` (AND) to make more complex conditions. There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as [Test if post is in a descendent category](http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category).
|
76 |
+
|
77 |
+
Remember -- the code runs even if the widget doesn't appear. (Even if it never appears!)
|
78 |
+
|
79 |
Examples:
|
80 |
|
81 |
+
* `is_home()` -- just the main blog page
|
82 |
+
* `!is_page('about')` -- everywhere EXCEPT this specific WP 'page'
|
83 |
+
* `is_category(array(5,9,10,11))` -- category page of one of the given category IDs
|
84 |
* `is_single() && in_category('baked-goods')` -- single post that's in the category with this slug
|
85 |
+
* `current_user_can('level_10')` -- admin only widget
|
86 |
* `strpos($_SERVER['HTTP_REFERER'], "google.com")!=false` -- widget to show when clicked through from a google search
|
87 |
* `is_category() && in_array($cat, get_term_children( 5, 'category'))` -- category page that's a descendent of category 5
|
88 |
* `global $post; return (in_array(77,get_post_ancestors($post)));` -- WP page that is a child of page 77
|
90 |
|
91 |
Note the extra ';' on the end where there is an explicit 'return'.
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
== The 'widget_content' filter ==
|
94 |
|
95 |
+
When this option is active (tick the option tickbox at the foot of the widget admin page) you can modify the text displayed by ANY widget from your own theme's functions.php file. Hook into the filter with:
|
96 |
|
97 |
`add_filter('widget_content', 'your_filter_function', [priority], 2);`
|
98 |
|
99 |
+
where `[priority]` is the optional priority parameter for the [add_filter](http://codex.wordpress.org/Function_Reference/add_filter) function. The filter function can take a second parameter (if you provde that last parameter '2') like this:
|
100 |
|
101 |
`function your_filter_function($content='', $widget_id='')`
|
102 |
|
106 |
|
107 |
I was motivated to make this filter in order to render all widget titles with the excellent [ttftitles plugin](http://templature.com/2007/10/18/ttftitles-wordpress-plugin/) like this:
|
108 |
|
109 |
+
`add_filter('widget_content', 'ttftext_widget_title');
|
110 |
+
function ttftext_widget_title($content='')
|
111 |
{ preg_match("/<h2[^>]*>([^<]+)/",$content, $matches);
|
112 |
$heading=$matches[1];
|
113 |
$insert_img=the_ttftext( $heading, false );
|
115 |
return $content;
|
116 |
}`
|
117 |
|
118 |
+
People often ask for a way to give widgets alternating styles. This filter inserts widget_style_a/widget_style_b into the class="widget ..." text usually found in a widget's main definition:
|
119 |
|
120 |
+
`add_filter('widget_content', 'make_alternating_widget_styles');
|
121 |
+
function make_alternating_widget_styles($content='')
|
122 |
{ global $wl_make_alt_ws;
|
123 |
$wl_make_alt_ws=($wl_make_alt_ws=="style_a")?"style_b":"style_a";
|
124 |
return preg_replace('/(class="widget )/', "$1 widget_${wl_make_alt_ws} ", $content);
|
127 |
|
128 |
== Changelog ==
|
129 |
|
130 |
+
= 0.47 =
|
131 |
+
FINALLY tracked down the elusive 'wp_reset_query' option resetting bug.
|
132 |
+
|
133 |
= 0.46 =
|
134 |
Fix to work with new WP2.8 admin ajax. With bonus fixes.
|
135 |
|
widget_logic.php
CHANGED
@@ -4,12 +4,19 @@ Plugin Name: Widget Logic
|
|
4 |
Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
|
5 |
Description: Control widgets with WP's conditional tags is_home etc
|
6 |
Author: Alan Trewartha
|
7 |
-
Version: 0.
|
8 |
Author URI: http://freakytrigger.co.uk/author/alan/
|
9 |
*/
|
10 |
|
11 |
|
12 |
add_filter( 'widget_update_callback', 'widget_logic_widget_update_callback', 10, 3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
// new in 2.8 - ajaxy update of a single widget
|
15 |
function widget_logic_widget_update_callback($instance, $new_instance, $this_widget)
|
@@ -36,7 +43,7 @@ function widget_logic_expand_control()
|
|
36 |
$wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
|
37 |
|
38 |
// clean up empty options (in PHP5 use array_intersect_key)
|
39 |
-
$regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values((array) $_POST['widget-id']),(
|
40 |
foreach (array_keys($wl_options) as $key)
|
41 |
if (!in_array($key, $regd_plus_new))
|
42 |
unset($wl_options[$key]);
|
4 |
Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
|
5 |
Description: Control widgets with WP's conditional tags is_home etc
|
6 |
Author: Alan Trewartha
|
7 |
+
Version: 0.47
|
8 |
Author URI: http://freakytrigger.co.uk/author/alan/
|
9 |
*/
|
10 |
|
11 |
|
12 |
add_filter( 'widget_update_callback', 'widget_logic_widget_update_callback', 10, 3);
|
13 |
+
add_filter( "plugin_action_links", "wl_charity", 10, 2);
|
14 |
+
|
15 |
+
function wl_charity($links, $file) {
|
16 |
+
if ($file == plugin_basename(__FILE__))
|
17 |
+
array_push($links, '<a href="http://www.justgiving.com/widgetlogic_cancerresearchuk/">Charity Donation</a>');
|
18 |
+
return $links;
|
19 |
+
}
|
20 |
|
21 |
// new in 2.8 - ajaxy update of a single widget
|
22 |
function widget_logic_widget_update_callback($instance, $new_instance, $this_widget)
|
43 |
$wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
|
44 |
|
45 |
// clean up empty options (in PHP5 use array_intersect_key)
|
46 |
+
$regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values((array) $_POST['widget-id']),array('widget_logic-options-filter', 'widget_logic-options-wp_reset_query'));
|
47 |
foreach (array_keys($wl_options) as $key)
|
48 |
if (!in_array($key, $regd_plus_new))
|
49 |
unset($wl_options[$key]);
|