Version Description
Fix to work with new WP2.8 admin ajax. With bonus fixes.
Download this release
Release Info
Developer | alanft |
Plugin | Widget Logic |
Version | 0.46 |
Comparing to | |
See all releases |
Code changes from version 0.45 to 0.46
- readme.txt +89 -41
- screenshot-1.png +0 -0
- screenshot-2.png +0 -0
- widget_logic.php +49 -27
readme.txt
CHANGED
@@ -1,32 +1,23 @@
|
|
1 |
=== Widget Logic ===
|
2 |
Contributors: alanft
|
|
|
3 |
Tags: widget, admin, conditional tags, filter
|
4 |
Requires at least: 2.5
|
5 |
-
Tested up to: 2.
|
6 |
-
Stable tag: 0.
|
7 |
|
8 |
-
Widget Logic lets you control
|
9 |
|
10 |
== Description ==
|
11 |
-
This plugin gives every widget an extra control field called "Widget logic" that lets you control
|
12 |
|
13 |
The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
|
14 |
|
15 |
-
There is also an option to add a wordpress 'widget_content' filter
|
16 |
|
17 |
-
=
|
18 |
-
0.44 - Officially works with 2.7 now. Documentation changes and minor bug fixes.
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
0.42 - WP 2.5+ only now. WP's widget admin has changed so much and I was getting tied up in knots trying to make it work with them both.
|
23 |
-
|
24 |
-
0.4 - Brings WP 2.5 compatibility. I am trying to make it back compatible. If you have trouble using WL with WP 2.1--2.3 let me know the issue.
|
25 |
-
|
26 |
-
0.31 - Last WP 2.3 only version
|
27 |
-
|
28 |
-
= Thanks To =
|
29 |
-
Kjetil Flekkoy for reporting and helping to diagnose errors in the 0.4 version
|
30 |
|
31 |
== Installation ==
|
32 |
|
@@ -42,34 +33,68 @@ Try switching to the WP default theme - if the problem goes away, there is somet
|
|
42 |
|
43 |
The most common sources of problems are:
|
44 |
|
45 |
-
* The logic text on one of your widgets
|
46 |
* Your theme doesn't call wp_head()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
== Screenshots ==
|
49 |
|
50 |
1. The 'Widget logic' field at work in standard widgets.
|
51 |
-
2. The
|
52 |
|
53 |
== Writing Logic Code ==
|
54 |
|
55 |
-
The text in the 'Widget logic' field can
|
56 |
|
57 |
-
|
58 |
|
59 |
Examples:
|
60 |
|
61 |
-
* `is_home()`
|
62 |
-
*
|
63 |
-
*
|
64 |
-
* `
|
65 |
-
* `current_user_can('level_10')`
|
66 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
== The 'widget_content' filter ==
|
71 |
|
72 |
-
Once this is active (tick the option tickbox at the foot of the widget admin page) you can modify the text displayed by
|
73 |
|
74 |
`add_filter('widget_content', 'your_filter_function', [priority], 2);`
|
75 |
|
@@ -77,15 +102,10 @@ where `[priority]` is the optional priority parameter for the [add_filter](http:
|
|
77 |
|
78 |
`function your_filter_function($content='', $widget_id='')`
|
79 |
|
80 |
-
The second parameter (widget_id) can be used to target specific widgets if needed.
|
81 |
|
82 |
_Example filters_
|
83 |
|
84 |
-
This adds the widget_id to the foot of every widget:
|
85 |
-
|
86 |
-
`function reveal_widget_id($content='', $widget_id='')
|
87 |
-
{ return $content."id=".$widget_id; }`
|
88 |
-
|
89 |
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:
|
90 |
|
91 |
`function ttftext_widget_title($content='', $widget_id='')
|
@@ -96,10 +116,38 @@ I was motivated to make this filter in order to render all widget titles with th
|
|
96 |
return $content;
|
97 |
}`
|
98 |
|
99 |
-
I
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
$content=str_replace("</h2>",$insert_rss."</h2>",$content);
|
104 |
-
}
|
105 |
-
`
|
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: 2.8
|
7 |
+
Stable tag: 0.46
|
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 |
|
11 |
== Description ==
|
12 |
+
This plugin gives every widget an extra control field called "Widget logic" that lets you control the pages that the widget will appear on.
|
13 |
|
14 |
The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
|
15 |
|
16 |
+
There is also an option to add a wordpress 'widget_content' filter -- this lets you tweak standard widgets to suit your theme without editing plugins and core code.
|
17 |
|
18 |
+
= Donations =
|
|
|
19 |
|
20 |
+
If you like and use Widget Logic you could consider a small donation to Cancer Research UK. I have a [JustGiving.com donation link](http://www.justgiving.com/widgetlogic_cancerresearchuk) above with the aim of raising 500 UKP in the next 12 months.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
== Installation ==
|
23 |
|
33 |
|
34 |
The most common sources of problems are:
|
35 |
|
36 |
+
* The logic text on one of your widgets is invalid PHP
|
37 |
* Your theme doesn't call wp_head()
|
38 |
+
* Your theme performs custom queries before calling the dynamic sidebar -- if so, try ticking the `wp_reset_query` option.
|
39 |
+
|
40 |
+
= Widgets appear when they shouldn't =
|
41 |
+
|
42 |
+
It might be that your theme performs custom queries before calling the sidebar. Try the `wp_reset_query` option.
|
43 |
+
|
44 |
+
Alternatively you may have not defined your logic tightly enough. For example when the sidebar is being processed, in_category('cheese') will be true if the last post on an archive page is in the 'cheese' category.
|
45 |
+
|
46 |
+
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:
|
54 |
+
|
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 |
|
62 |
== Screenshots ==
|
63 |
|
64 |
1. The 'Widget logic' field at work in standard widgets.
|
65 |
+
2. The `widget_content` filter and `wp_reset_query` options are at the foot of the widget admin page. (Both are off by default.)
|
66 |
|
67 |
== Writing Logic Code ==
|
68 |
|
69 |
+
The text in the 'Widget logic' field can be full PHP code and should return 'true' when you need the widget to appear. Make good use of [WP's own conditional tags](http://codex.wordpress.org/Conditional_Tags).
|
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/home page
|
76 |
+
* `is_page('about')` -- WP 'page' with the given URL slug
|
77 |
+
* `!is_category(array(5,9,10,11))` -- category page of one of the given category IDs
|
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
|
83 |
+
* `global $post; return (is_page('home') || ($post->post_parent=="13"));` -- home page OR the page that's a child of page 13
|
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 |
+
Once 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:
|
98 |
|
99 |
`add_filter('widget_content', 'your_filter_function', [priority], 2);`
|
100 |
|
102 |
|
103 |
`function your_filter_function($content='', $widget_id='')`
|
104 |
|
105 |
+
The second parameter ($widget_id) can be used to target specific widgets if needed.
|
106 |
|
107 |
_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 |
`function ttftext_widget_title($content='', $widget_id='')
|
116 |
return $content;
|
117 |
}`
|
118 |
|
119 |
+
I often see people asking for ways 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:
|
120 |
+
|
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);
|
125 |
+
}`
|
126 |
+
|
127 |
+
|
128 |
+
== Changelog ==
|
129 |
+
|
130 |
+
= 0.46 =
|
131 |
+
Fix to work with new WP2.8 admin ajax. With bonus fixes.
|
132 |
+
|
133 |
+
= 0.44 =
|
134 |
+
Officially works with 2.7 now. Documentation changes and minor bug fixes.
|
135 |
+
|
136 |
+
= 0.43 =
|
137 |
+
simple bug fix (form data was being lost when 'Cancel'ing widgets)
|
138 |
+
|
139 |
+
= 0.42 =
|
140 |
+
WP 2.5+ only now. WP's widget admin has changed so much and I was getting tied up in knots trying to make it work with them both.
|
141 |
+
|
142 |
+
= 0.4 =
|
143 |
+
Brings WP 2.5 compatibility. I am trying to make it back compatible. If you have trouble using WL with WP 2.1--2.3 let me know the issue. Thanks to Kjetil Flekkoy for reporting and helping to diagnose errors in this version
|
144 |
+
|
145 |
+
= 0.31 =
|
146 |
+
Last WP 2.3 only version
|
147 |
+
|
148 |
+
== Upgrade Notice ==
|
149 |
+
= 0.46 =
|
150 |
+
Required with WP2.8 cos of changes in Widget admin AJAX
|
151 |
|
152 |
+
= 0.44 =
|
153 |
+
Updated for WP2.7 with extra bug fixes
|
|
|
|
|
|
screenshot-1.png
CHANGED
Binary file
|
screenshot-2.png
CHANGED
Binary file
|
widget_logic.php
CHANGED
@@ -4,11 +4,23 @@ 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 |
|
13 |
add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
|
14 |
|
@@ -29,31 +41,30 @@ function widget_logic_expand_control()
|
|
29 |
if (!in_array($key, $regd_plus_new))
|
30 |
unset($wl_options[$key]);
|
31 |
}
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
array_push($wp_registered_widget_controls[$id]['params'],$id);
|
44 |
-
$wp_registered_widget_controls[$id]['height']+=40; // this is really a pre2.5 thing - discard?
|
45 |
-
}
|
46 |
-
|
47 |
-
// do the redirection
|
48 |
-
$wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
|
49 |
-
$wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
|
50 |
}
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
|
|
|
53 |
// check the 'widget content' filter option
|
54 |
if ( isset($_POST['widget_logic-options-submit']) )
|
55 |
-
|
56 |
-
|
|
|
57 |
update_option('widget_logic', $wl_options);
|
58 |
}
|
59 |
|
@@ -67,8 +78,13 @@ function widget_logic_options_filter()
|
|
67 |
<form method="POST">
|
68 |
<h2>Widget Logic options</h2>
|
69 |
<p style="line-height: 30px;">
|
70 |
-
|
|
|
71 |
<input id="widget_logic-options-filter" name="widget_logic-options-filter" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-filter'] ?> /></label>
|
|
|
|
|
|
|
|
|
72 |
<span class="submit"><input type="submit" name="widget_logic-options-submit" id="widget_logic-options-submit" value="Save" /></span></p>
|
73 |
</form>
|
74 |
</div>
|
@@ -111,15 +127,18 @@ add_action('wp_head', 'widget_logic_redirect_callback');
|
|
111 |
function widget_logic_redirect_callback()
|
112 |
{ global $wp_registered_widgets;
|
113 |
foreach ( $wp_registered_widgets as $id => $widget )
|
114 |
-
{
|
115 |
-
$wp_registered_widgets[$id]['
|
116 |
-
|
|
|
|
|
117 |
}
|
118 |
}
|
119 |
|
120 |
// the redirection comes here
|
121 |
function widget_logic_redirected_callback()
|
122 |
-
{ global $wp_registered_widgets;
|
|
|
123 |
$params=func_get_args(); // get all the passed params
|
124 |
$id=array_pop($params); // take off the widget ID
|
125 |
$callback=$wp_registered_widgets[$id]['callback_wl_redirect']; // find the real callback
|
@@ -128,6 +147,10 @@ function widget_logic_redirected_callback()
|
|
128 |
$wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
|
129 |
$wl_value=(stristr($wl_value, "return"))?$wl_value:"return (".$wl_value.");";
|
130 |
|
|
|
|
|
|
|
|
|
131 |
$wl_value=(eval($wl_value) && is_callable($callback));
|
132 |
if ( $wl_value )
|
133 |
{ if ($wl_options['widget_logic-options-filter']!='checked')
|
@@ -141,5 +164,4 @@ function widget_logic_redirected_callback()
|
|
141 |
}
|
142 |
}
|
143 |
}
|
144 |
-
|
145 |
?>
|
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.46
|
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)
|
16 |
+
{ $widget_id=$this_widget->id;
|
17 |
+
if ( isset($_POST[$widget_id.'-widget_logic']))
|
18 |
+
{ if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
|
19 |
+
$wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
|
20 |
+
update_option('widget_logic', $wl_options);
|
21 |
+
}
|
22 |
+
return $instance;
|
23 |
+
}
|
24 |
|
25 |
add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
|
26 |
|
41 |
if (!in_array($key, $regd_plus_new))
|
42 |
unset($wl_options[$key]);
|
43 |
}
|
44 |
+
|
45 |
+
foreach ( $wp_registered_widgets as $id => $widget )
|
46 |
+
{ if (!$wp_registered_widget_controls[$id])
|
47 |
+
wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
|
48 |
+
|
49 |
+
if (!array_key_exists(0,$wp_registered_widget_controls[$id]['params']) || is_array($wp_registered_widget_controls[$id]['params'][0]))
|
50 |
+
$wp_registered_widget_controls[$id]['params'][0]['id_for_wl']=$id;
|
51 |
+
else
|
52 |
+
{ // some older widgets put number in to params directly (which messes up the 'templates' in WP2.5)
|
53 |
+
array_push($wp_registered_widget_controls[$id]['params'],$id);
|
54 |
+
$wp_registered_widget_controls[$id]['height']+=40; // this is really a pre2.5 thing - discard?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
56 |
+
|
57 |
+
// do the redirection
|
58 |
+
$wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
|
59 |
+
$wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
|
60 |
}
|
61 |
|
62 |
+
|
63 |
// check the 'widget content' filter option
|
64 |
if ( isset($_POST['widget_logic-options-submit']) )
|
65 |
+
{ $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
|
66 |
+
$wl_options['widget_logic-options-wp_reset_query']=$_POST['widget_logic-options-wp_reset_query'];
|
67 |
+
}
|
68 |
update_option('widget_logic', $wl_options);
|
69 |
}
|
70 |
|
78 |
<form method="POST">
|
79 |
<h2>Widget Logic options</h2>
|
80 |
<p style="line-height: 30px;">
|
81 |
+
|
82 |
+
<label for="widget_logic-options-filter" title="Adds a new WP filter you can use in your own code. Not needed for main Widget Logic functionality.">Use 'widget_content' filter
|
83 |
<input id="widget_logic-options-filter" name="widget_logic-options-filter" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-filter'] ?> /></label>
|
84 |
+
|
85 |
+
<label for="widget_logic-options-wp_reset_query" title="Resets a theme's custom queries before your Widget Logic is checked.">Use 'wp_reset_query' fix
|
86 |
+
<input id="widget_logic-options-wp_reset_query" name="widget_logic-options-wp_reset_query" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-wp_reset_query'] ?> /></label>
|
87 |
+
|
88 |
<span class="submit"><input type="submit" name="widget_logic-options-submit" id="widget_logic-options-submit" value="Save" /></span></p>
|
89 |
</form>
|
90 |
</div>
|
127 |
function widget_logic_redirect_callback()
|
128 |
{ global $wp_registered_widgets;
|
129 |
foreach ( $wp_registered_widgets as $id => $widget )
|
130 |
+
{ if (!$wp_registered_widgets[$id]['callback_wl_redirect'])
|
131 |
+
{ array_push($wp_registered_widgets[$id]['params'],$id);
|
132 |
+
$wp_registered_widgets[$id]['callback_wl_redirect']=$wp_registered_widgets[$id]['callback'];
|
133 |
+
$wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
|
134 |
+
}
|
135 |
}
|
136 |
}
|
137 |
|
138 |
// the redirection comes here
|
139 |
function widget_logic_redirected_callback()
|
140 |
+
{ global $wp_registered_widgets, $wp_reset_query_is_done;
|
141 |
+
|
142 |
$params=func_get_args(); // get all the passed params
|
143 |
$id=array_pop($params); // take off the widget ID
|
144 |
$callback=$wp_registered_widgets[$id]['callback_wl_redirect']; // find the real callback
|
147 |
$wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
|
148 |
$wl_value=(stristr($wl_value, "return"))?$wl_value:"return (".$wl_value.");";
|
149 |
|
150 |
+
// before we execute the condtional code, perhaps we want to wp_reset_query...
|
151 |
+
if ($wl_options['widget_logic-options-wp_reset_query']=='checked' && !$wp_reset_query_is_done)
|
152 |
+
{ wp_reset_query(); $wp_reset_query_is_done=true; }
|
153 |
+
|
154 |
$wl_value=(eval($wl_value) && is_callable($callback));
|
155 |
if ( $wl_value )
|
156 |
{ if ($wl_options['widget_logic-options-filter']!='checked')
|
164 |
}
|
165 |
}
|
166 |
}
|
|
|
167 |
?>
|