Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Autoload isn't the problem. One plugin is: how I measure cost per plugin

Site Health told me the site had a problem. It didn't tell me whose problem it was. Since WordPress 6.6, Site Health has a check called "Autoloaded options could affect performance". It adds up every autoloaded option a

Site Health told me the site had a problem. It didn't tell me whose problem it was.

Since WordPress 6.6, Site Health has a check called "Autoloaded options could affect performance". It adds up every autoloaded option and, once the total reaches 800,000 bytes, flags it as a critical issue. That's useful. But in every slow site I've looked at, the next question is always the same: which plugin put that data there, and what else is it costing me?

A single number can't answer that. So I stopped looking at totals and started building a cost-per-plugin table: one row per plugin, with its autoloaded bytes, its own database tables, its cron events and its scheduled actions. Here's how to do it by hand with SQL and WP-CLI, and the traps I fell into along the way.

Step 0: know what "autoloaded" means after 6.6

WordPress 6.6 changed the values in the autoload column. Old sites still have yes and no; newer writes use on, off, auto, auto-on and auto-off. Core treats four of them as autoloaded: yes, on, auto-on and auto (see wp_autoload_values_to_autoload()).

If your query still says WHERE autoload = 'yes', you're missing part of the picture on any site that has been updated since mid-2024.

One more 6.6 detail worth knowing: when a plugin saves an option without an explicit autoload value and the serialized value is larger than 150,000 bytes, core now stores it as not autoloaded. That protects you from new giant options. It does nothing for the ones already sitting in your table.

Step 1: find the heavy options

SELECT option_name,
       LENGTH(option_value) AS bytes,
       autoload
FROM   wp_options
WHERE  autoload IN ('yes', 'on', 'auto-on', 'auto')
ORDER  BY bytes DESC
LIMIT  30;

(Change wp_ to your table prefix.) With a recent WP-CLI you can get the total in one line, and it already counts auto and auto-on:

wp option list --autoload=on --format=total_bytes

The total from SQL won't match Site Health to the byte. Site Health measures the values PHP actually loads (wp_load_alloptions()), not the rows in the table. Close enough to find the culprits, not close enough to argue about the last kilobyte.

Step 2: attribute each option to a plugin

This is where totals turn into names. For each heavy option, I try three things in order:

  1. Prefix. Most plugins prefix their options (wc_, woocommerce_, yourplugin_settings). Group by the first segment of option_name and you'll already see clusters.
  2. Code search. Prefixes lie, so confirm by searching the code for the exact option name:
   grep -rl "my_heavy_option" wp-content/plugins wp-content/themes wp-content/mu-plugins
  1. Registered callbacks. Some options are written by a class whose name doesn't match the option. If the grep finds nothing, look for the string built dynamically ($prefix . '_cache') before you give up.

And here's the rule I wish I'd started with: if two plugins could own an option, I don't pick one. It goes into an "unattributed" row. A wrong attribution sends someone to uninstall the wrong plugin, which is worse than admitting you don't know.

Step 3: autoload is only one column

Once you have names, the interesting part is that the heaviest plugin by autoload is often not the heaviest plugin overall. I add three more columns.

Own tables. Most plugins that create tables use a recognisable prefix after the WordPress one:

SELECT table_name,
       ROUND((data_length + index_length) / 1024 / 1024, 1) AS mb,
       table_rows
FROM   information_schema.tables
WHERE  table_schema = DATABASE()
  AND  table_name LIKE 'wp\_%'
ORDER  BY (data_length + index_length) DESC;

On one multilingual store I audited, the table column changed the conversation: a security plugin had 25 tables taking about 25 MB, and the translation plugin had another 25 tables taking over 33 MB. That's weight no autoload check will ever show you.

Watch out for case, too. Not every plugin names its tables in lowercase, and whether a comparison is case-sensitive depends on where you do it. I lowercase both sides before matching prefixes, after an audit where a whole set of tables was silently assigned to the wrong owner.

Cron events.

wp cron event list --fields=hook,next_run_relative,recurrence

For each hook, run the same grep as above. A hook that no installed code registers is an orphan: it fires on schedule and does nothing except cost a request. A hook that's overdue by hours tells you WP-Cron isn't running properly, and that's a hosting problem, not a plugin problem.

Scheduled actions. If the site runs WooCommerce (or any plugin that bundles Action Scheduler), failed and overdue actions matter more than cron:

SELECT hook, status, COUNT(*) AS actions
FROM   wp_actionscheduler_actions
WHERE  status = 'failed'
   OR (status = 'pending' AND scheduled_date_gmt < UTC_TIMESTAMP())
GROUP  BY hook, status
ORDER  BY actions DESC;

The three traps

1. Leftovers. Deleting a plugin often leaves its data behind. On older sites, a good chunk of autoloaded bytes belongs to plugins that are no longer installed. I give them their own row ("leftovers: no installed code uses them") instead of hiding them in the total, because they're the safest thing to clean.

2. Shared libraries. Action Scheduler ships inside WooCommerce and several other plugins, and only one copy runs. Blaming its tables on whichever plugin happens to load it is a classic mistake. It gets its own row as a shared library.

3. Inactive plugins. An inactive plugin's code doesn't run, but its autoloaded options are still loaded on every request. "I deactivated it" isn't the same as "it costs nothing".

What I do with the table

The table answers the question that Site Health can't: which plugin, and how much. Then the fixes are usually boring:

  • Leftovers: export, then delete.
  • Big options that are only needed in wp-admin: switch them off autoload. Core has had wp_set_option_autoload_values() since 6.4, and WP-CLI has wp option set-autoload <option> off. Test it on staging first: some plugins assume their option is always in memory.
  • Orphaned cron hooks: unschedule them.
  • Failed scheduled actions: read the error first. The count is a symptom, not the bug.

I also wrote a longer guide on what autoload is and how much is too much. It's in Spanish, but the SQL works in any language.

If you have a site where the cost-per-plugin table surprised you, I'd like to hear which column did it.

— Juande

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.