Opened 9 years ago
Closed 9 years ago
#33292 closed defect (bug) (duplicate)
Never-used widgets generate database queries
Reported by: | dlh | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Widgets | Keywords: | has-patch dev-feedback |
Focuses: | Cc: |
Description
When WP_Widget::_register()
runs, it calls WP_Widget::get_settings()
, which then calls get_option( $this->option_name )
.
By default, widget options are set to autoload, so the get_option()
call can fetch the loaded value. But a widget that has never been used doesn't have an option, so get_option()
runs all the way through, including the database query.
This can be tested on a fresh install. Each frontend request includes SELECT option_value FROM wp_options WHERE option_name = 'widget_pages' LIMIT 1
, but that option doesn't exist (same for widget_tag_cloud
, widget_calendar
, and widget_nav_menu
).
Assuming everything so far is accurate, the attached patch would have get_settings()
call WP_Widget::save_settings()
with an empty array when no option is found in the database and the alt_option_name
property isn't set. On the next request, the widget option would be autoloaded and not generate another query.
@dlh: Your patch looks like a good alternate approach to what has been proposed on #26876. Please join in there.