I am creating a plugin in which my custom sidebar widget changes content depending on the page it is loading on. One way to do this is
// Register and load the widget
function custom_register_widget() {
register_widget( 'custom_widget' );
}
//trigger on every sidebar load
add_action('dynamic_sidebar', 'custom_register_widget' );
However, this calls the register_widget() on every page load ( thereby making changes to WordPress DB ), thus slowing the page speed.
Is there an efficient way to this?
register_widget
does not make changes to the database. Also remember that ifdynamic_sidebar
runs every time a sidebar is loaded, and you have more than one sidebar, you'll get duplication. And if no sidebar is displayed, the widget is never registered, and won't be available in the widget admin