Make WordPress Core

Opened 16 years ago

Closed 16 years ago

#10136 closed defect (bug) (fixed)

is_active_sidebar() should use wp_get_sidebars_widgets()

Reported by: greenshady's profile greenshady Owned by: azaozz's profile azaozz
Milestone: 2.8.1 Priority: normal
Severity: normal Version: 2.8
Component: Widgets Keywords: has-patch tested
Focuses: Cc:

Description

Some themes and plugins use the sidebars_widgets filter hook to disable a particular sidebar (see: http://justintadlock.com/archives/2009/03/06/disable-widget-areas-without-touching-theme-templates).

This gives themes/plugins more flexibility, which is what I'm sure we were aiming for with the is_active_sidebar() function. But, since is_active_sidebar() uses get_option() to check for sidebars, the sidebars_widgets filter hook is bypassed.

What I propose is a one-line change of:

$sidebars_widgets = get_option( 'sidebars_widgets', array() );

To:

$sidebars_widgets = wp_get_sidebars_widgets();

I haven't had any issues with this change. And, I can think of no other ways to disable a sidebar.

Attachments (1)

10136.diff (543 bytes) - added by Denis-de-Bernardy 16 years ago.

Download all attachments as: .zip

Change History (15)

#1 @Denis-de-Bernardy
16 years ago

  • Milestone 2.8.1 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

use the unregister_widget() function instead.

#2 @greenshady
16 years ago

  • Milestone set to 2.8.1
  • Resolution wontfix deleted
  • Status changed from closed to reopened

I was under the impression that unregister_widget() literally unregisters a widget, making it not available for use. That's what I've been using it for.

What I'm taking about is having the ability to disable a sidebar when I need to or a theme/plugin user's sidebar.

A function like this should do it:

add_filter( 'sidebars_widgets', 'disable_primary_sidebar' );

function disable_primary_sidebar( $sidebars_widgets ) {
	if ( is_home() )
		$sidebars_widgets['primary'] = false;

	return $sidebars_widgets;
}

That disables the primary sidebar on the home page. I'm sorry, but I don't see how this can be accomplished with unregister_widget().

#3 @Denis-de-Bernardy
16 years ago

  • Keywords needs-patch close added
  • Type changed from defect (bug) to enhancement

Sorry, read this a bit too quickly. There also are unregister_sidebar() and unregister_sidebar_widget(), btw. At any event:

  • wp_sidebars_widgets() is slower than is needed here
  • we may get side effects due to the built-in recent comments widget (or is that recent posts?), which calls it as part of the widget
  • there are several plugins that let you disable a widget on a per context basis, and these plugins can now access the sidebar (it's $args[id])

Suggesting wontfix either way.

#4 @Denis-de-Bernardy
16 years ago

  • Milestone changed from 2.8.1 to 2.9

#5 @greenshady
16 years ago

Maybe I haven't completely explained this well enough. I don't consider this an enhancement. I consider it a bug with the current implementation.

If someone sets a sidebar to FALSE through the sidebars_widgets filter hook, then is_active_sidebar() should return FALSE for that particular sidebar, but it does not. It returns TRUE.

To me, this is a bug. If no one else considers this a bug, then I don't see a reason for having the is_active_sidebar() conditional tag at all.

The focus of this ticket has been too much on using wp_get_sidebars_widgets() rather than finding a solution. If that function is too slow, then a new filter hook needs to be added within is_active_sidebar() or a new function created.

  • unregister_sidebar() won't work because is_active_widget() will still return TRUE.
  • unregister_sidebar_widget() won't work because we're talking about the entire sidebar and not any widget in particular.
  • All of the plugins I've encountered won't work because they only conditionally show/hide a widget and is_active_sidebar() still returns TRUE even when no widgets are being shown. In fact, they might work better if is_active_sidebar() could be filtered somehow.

#6 @Denis-de-Bernardy
16 years ago

There are two hooks you can use already: pre_option_sidebars_widgets, and option_sidebars_widgets. But adding a patch nonetheless.

#7 @Denis-de-Bernardy
16 years ago

  • Keywords has-patch added; needs-patch removed

#8 @Denis-de-Bernardy
16 years ago

  • Keywords tested added

#9 @Denis-de-Bernardy
16 years ago

  • Type changed from enhancement to defect (bug)

#10 @azaozz
16 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [11562]) is_active_sidebar() should use wp_get_sidebars_widgets(), props greenshady and Denis-de-Bernardy, fixes #10136

#11 @kretzschmar
16 years ago

  • Milestone changed from 2.9 to 2.8.1

Please add this fix to the 2.8.1 release.

#12 @Denis-de-Bernardy
16 years ago

  • Keywords close removed

Please don't change the milestone of fixed bugs without re-opening them.

#13 @Denis-de-Bernardy
16 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#14 @azaozz
16 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [11580]) is_active_sidebar() should use wp_get_sidebars_widgets(), props greenshady and Denis-de-Bernardy, fixes #10136 for 2.8.1

Note: See TracTickets for help on using tickets.