Opened 2 years ago
Last modified 2 years ago
#17201 new enhancement
dynamic_sidebar performance
| Reported by: |
|
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)
Change History (4)
Note: See
TracTickets for help on using
tickets.

Testing the code seems that
$sidebars_widgets = wp_get_sidebars_widgets();
need to be moved on top of the function also.