Performance PHP Intermediate

Stop a Large Option From Autoloading

Turn off autoloading for big options that WordPress loads on every request but rarely needs.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 20 lines
add_action( 'admin_init', function (): void {
	// Run once. Remove this line's option to run again.
	if ( get_option( 'nahnu_autoload_fixed' ) ) {
		return;
	}

	// Option names from Site Health or the query in the notes.
	$options = [
		'some_plugin_big_cache',
		'old_plugin_settings',
	];

	foreach ( $options as $name ) {
		if ( false !== get_option( $name ) ) {
			wp_set_option_autoload( $name, false );
		}
	}

	update_option( 'nahnu_autoload_fixed', 1, false );
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress loads every “autoloaded” option on every page request. Plugins sometimes store large caches or old data with autoload on, so every page pays the cost. Since WordPress 6.6, Site Health warns when autoloaded options get too big. This snippet switches autoloading off for the ones you name.

How it works

wp_set_option_autoload() changes an option’s autoload flag without changing its value. The option still works; WordPress loads it the first time something asks for it instead of on every request.

The snippet runs once on admin_init and records that it ran, so it doesn’t touch the database on every admin page.

Things to watch

  • Find large options with Site Health, or with wp db query "SELECT option_name, LENGTH(option_value) AS size FROM wp_options WHERE autoload IN ('yes','on','auto-on') ORDER BY size DESC LIMIT 20".
  • Some plugins switch autoloading back on when they update an option. Check again after plugin updates.
  • Don’t change WordPress core options like siteurl, active_plugins or cron.

How to test it

Load wp-admin once, then check the option’s autoload value in the database. It should now be off, and Site Health’s autoload warning should shrink.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only