Restricting WordPress Widgets Per Category

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Restricting WordPress Widgets Per Category

Post by volomike »

I do custom WordPress theming. I have an unusual client request. He wants to take all the default sidebar widgets and restrict them to category, such as Calendar, Tag Cloud, Recent Comments, etc. Anyone know how to do that?

I found that I can use the add_filter() hook API such that I can intercept all SQL queries in sidebar.php, and adjust them with:

Code: Select all

add_filter('query','myQueryFilterCallback');
function myQueryFilterCallback($args) {
print_r($args);
echo "<br />"
return $args;
}
The only trouble is, I cannot predict what widget a given query is running for. Besides, editing pure SQL is a bit risky because during upgrades of WP the query could be dramatically different.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Restricting WordPress Widgets Per Category

Post by greyhoundcode »

When you say the "default sidebar widgets", do you mean the usual stuff that is outputted in a typical theme following:

Code: Select all

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
It's an interesting problem, my instincts are that there is no easy fix for achieving this from within a theme, though there may well be a very complicated fix.

I'm sure what I'm about to propose is exactly what you wish to avoid, but in this case might it not be easiest to copy each of the relevant widgets code into a new plugin, amending the queries appropriately, then use the new template tags that the plugin generates within your theme, if that makes sense?

Inelegant I know.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Restricting WordPress Widgets Per Category

Post by volomike »

greyhound, your instincts were right. The best choice appears to be to follow this method:

http://stackoverflow.com/questions/2346 ... o-category

and more importantly:

http://stackoverflow.com/questions/2346 ... n-category
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Restricting WordPress Widgets Per Category

Post by josh »

Stack overflow lets you answer (and even accept answers for?) your own questions?.. weird
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Restricting WordPress Widgets Per Category

Post by matthijs »

Maybe I don't understand the problem, but can't you do what you want with conditional tags?
http://codex.wordpress.org/Conditional_Tags

is_category(), is_home(), !in_category( array( 4,5,6 ) ) , etc

Then show what you want depending on the condition
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Restricting WordPress Widgets Per Category

Post by greyhoundcode »

Volomike at StackOverflow wrote:the WordPress 3.0 release is coming fairly soon and may introduce some dramatic changes.
That's for sure. Especially if clients have administrator permissions and can click on the "automatically update to the latest version" link, potentially causing breakages elsewhere.
Post Reply