Opened 2 years ago

Last modified 2 years ago

#17201 new enhancement

dynamic_sidebar performance

Reported by: mrubiolvn Owned by:
Priority: normal Milestone: Awaiting Review
Component: Widgets Version: 3.1
Severity: normal Keywords: has-patch dev-feedback
Cc:

Description

I've got a few dynamic sidebars (say 6 or 7) and the dynamic_sidebar function spends 1/4 of a second only calling sanitize_title.

See the piece of code on wp-includes/widgets.php:

	if ( is_int($index) ) {
		$index = "sidebar-$index";
	} else {
		$index = sanitize_title($index);
		foreach ( (array) $wp_registered_sidebars as $key => $value ) {
			if ( sanitize_title($value['name']) == $index ) {
				$index = $key;
				break;
			}
		}
	}

That's occurs evenf if you provide an id, and not the sidebar name.
We could avoid that by checking before trying to use the sidebar name if a sidebar exists with that id.

Like so...

	if ( is_int($index) ) {
		$index = "sidebar-$index";
	} elseif ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) ) {
		$index = sanitize_title($index);
		foreach ( (array) $wp_registered_sidebars as $key => $value ) {
			if ( sanitize_title($value['name']) == $index ) {
				$index = $key;
				break;
			}
		}
	}

Attachments (1)

revision.diff (1.0 KB) - added by mrubiolvn 2 years ago.

Download all attachments as: .zip

Change History (4)

Testing the code seems that

$sidebars_widgets = wp_get_sidebars_widgets();

need to be moved on top of the function also.

  • Keywords has-patch added
  • Keywords dev-feedback added
  • Summary changed from sanitize_title on dynamic_sidebar: performance to dynamic_sidebar performance

I'd like this fixed, don't know what else you need. Maybe a fancier ticket title... there it goes :) Thanks.

Note: See TracTickets for help on using tickets.