Make WordPress Core

Opened 11 years ago

Last modified 13 days ago

#24283 new defect (bug)

is_active_widget() incorrect logic

Reported by: valllabh's profile valllabh Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Widgets Keywords: dev-feedback
Focuses: Cc:

Description

is_active_widget() only returns 'true' only if widget used inside sidebar.

But there is a provision to use widget directly using the_widget() function. I believe there is a need of alter the logic of just checking inside sidebars.

Change History (3)

#1 @alex-ye
11 years ago

  • Cc nashwan.doaqan@… added

#2 @csixty4
11 years ago

  • Keywords dev-feedback added

I'm not sure this can be addressed practically. is_active_widget() just checks to see if a widget is assigned to a sidebar, and the reporter is right that the_widget() can render an arbitrary widget anywhere in a theme or plugin.

If a widget is assigned to a sidebar, then is_active_widget() knows its state at start of rendering the page. But if something calls is_active_widget() on a widget before the_widget() has been called to render it, WordPress can't see into the future.

A couple possible solutions:

  1. Keep the existing code as-is
  2. Deprecate is_active_widget() and rename it to is_widget_assigned_to_sidebar() or something does the exact same thing, but better reflects what it tests.
  3. Make is_active_widget() return true if the_widget() has been called for a widget type in the current page so far. I could see this being confusing.
  4. Provide a way for themes & plugins to declare what widgets they invoke directly. But that sets a precedent I'm not comfortable with, and the widgets can be rendered on a per-page basis which makes this kind of moot.

#3 @gingerbrad
13 days ago

below script would be helpful:

function is_active_widget_extended( $widget_id ) {

global $wp_registered_widgets;

foreach ( $wp_registered_widgets as $widget ) {

if ( $widget_id == $widgetid? && is_active_widget( false, false, $widgetid?, true ) ) {

return true;

}

}

ob_start();
the_widget( $widget_id );
$widget_output = ob_get_clean();

if ( ! empty( $widget_output ) ) {

return true;

}

return false;

}

Note: See TracTickets for help on using tickets.