Skip to content

Lesson 8 of 9 · 26m

Performance, Compatibility, and Debugging in Hostile Environments

How plugins behave under object cache, page cache, opcode cache, old PHP assumptions, other plugins, and incomplete diagnostics — plus how to debug without guessing.

Your plugin will not run on a clean local install in production. It will run behind Cloudflare, inside a page cache, on hosts that disable cron, beside a page builder, under a must-use plugin that rewrites output, and with memory limits tuned by someone you will never meet. Compatibility engineering means assuming interference and designing for observability.

Performance budgets start at bootstrap

Measure plugin load cost before features run. The first red flags are universal:

  • loading every PHP file on every request
  • querying options one at a time
  • instantiating remote clients before they are needed
  • autoloading large configuration arrays
  • registering expensive callbacks on universally-run hooks

Use lazy initialization. Register hooks cheaply, then construct services inside the callback. If a feature only runs in wp-admin, do not bootstrap it on the frontend. If it only runs for product pages, detect that as late as necessary but as early as practical.

Caching is contextual, not binary

$key = 'acme_inventory_' . $warehouse_id;
$data = wp_cache_get($key, 'acme');
if (false === $data) {
    $data = acme_fetch_inventory($warehouse_id);
    wp_cache_set($key, $data, 'acme', 300);
}

On hosts with persistent object cache, wp_cache_* is backed by Redis or Memcached. Elsewhere it lasts only for the request. Design correctness so either implementation works. Transients add database persistence, but they are not guaranteed durable on all hosts and can be externally cached in surprising ways. They are a cache, not source of truth.

Page caching means any output personalized per user must either bypass cache correctly or be rendered client-side after a cache-safe page load. Never vary output secretly based on cookies unless you control the cache keys.

Compatibility: assume other code is present

Namespace functions and classes, prefix option names, prefix cron hooks, and avoid replacing core behavior globally. Hooks can be added more than once when plugins are included twice or opcode caches behave strangely. Idempotent registration matters.

if (!has_action('init', 'acme_register_types')) {
    add_action('init', 'acme_register_types');
}

Avoid checking plugin versions by string parsing alone. Feature-detect where possible, and fail closed with a clear admin notice when requirements are unmet.

Debugging without guessing

The minimum professional toolkit:

  • WP_DEBUG_LOG writes to wp-content/debug.log when enabled.
  • Query Monitor shows hooks, queries, cache behavior, HTTP calls, and template loading.
  • WP-CLI gives repeatable execution outside browser timeouts.
  • wp_remote_post() responses must be inspected for WP_Error, status code, and body.
if (defined('WP_DEBUG') && WP_DEBUG) {
    error_log('acme sync skipped: ' . $reason);
}

Do not leave noisy debug logging in production. Log operational events that matter, with correlation IDs, bounded message sizes, and no secrets.

Build failure modes users can act on

“Something went wrong” is a dead end. Better errors name the failed subsystem, the safe next action, and whether data was changed:

  • Inventory sync stopped before committing changes because the warehouse API returned 401. Re-enter credentials under Settings → Inventory.
  • Batch 183 processed 500 of 2,400 rows and will resume automatically.

Field notes

  • A fatal error in a callback can break the whole site. Catch Throwable at integration boundaries and report failures safely.
  • PHP version support is a business decision. Do not use syntax newer than your declared minimum.
  • Never trust browser-only reproduction. Test WP-CLI, cron, REST, and object-cache-enabled paths.
  • The plugin conflict is real until proven otherwise; reproduce with a controlled plugin list instead of accusing the loudest neighbor.

Was this lesson helpful?

Sign in to rate