Post

Safe WordPress Plugin Checklist

Checklist for building WordPress plugins that fail gracefully instead of white-screening the site.

Goal: if something goes wrong, the plugin switches itself off and tells the admin. The site stays up.

Checklist

  • Declare dependencies in the plugin header
  • Check function and class existence before using them
  • Return WP_Error for internal failures (don’t die, don’t fatal)
  • Wrap risky operations in try/catch
  • Show failures as safe admin notices
  • Test with the dependency deactivated

1. Declare dependencies in the plugin header

WordPress 6.5+ blocks activation if a required plugin is missing or inactive.

1
2
3
4
5
6
7
<?php
/**
 * Plugin Name:       My Plugin
 * Requires at least: 6.5
 * Requires PHP:      8.0
 * Requires Plugins:  woocommerce, advanced-custom-fields
 */
  • Requires Plugins takes wp.org slugs, comma separated.
  • Only works for plugins hosted on wordpress.org. Premium plugins (e.g. ACF Pro) still need check #2.
  • It does not stop a dependency being deactivated later via code or wp-cli, so keep the runtime checks.

2. Check function and class existence

Never assume another plugin is loaded. Hook late and check first.

1
2
3
4
5
6
7
8
9
10
11
add_action( 'plugins_loaded', 'myplugin_init' );

function myplugin_init() {
	if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'get_field' ) ) {
		add_action( 'admin_notices', 'myplugin_missing_deps_notice' );
		return; // stop here, load nothing else
	}

	require_once __DIR__ . '/includes/class-myplugin.php';
	new MyPlugin();
}
  • Use plugins_loaded (or init), not top-level file code. Load order isn’t guaranteed at file include time.
  • Check what you actually call: function_exists(), class_exists(), defined() for constants.
  • Also guard your own function names with a unique prefix to avoid redeclare fatals.

3. Use WP_Error for internal failures

Return an error object. Let the caller decide what to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function myplugin_get_data( $id ) {
	$response = wp_remote_get( "https://api.example.com/items/{$id}" );

	if ( is_wp_error( $response ) ) {
		return $response; // pass it up
	}

	if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
		return new WP_Error( 'myplugin_bad_response', 'API did not return 200.' );
	}

	return json_decode( wp_remote_retrieve_body( $response ), true );
}

$data = myplugin_get_data( 5 );
if ( is_wp_error( $data ) ) {
	error_log( 'myplugin: ' . $data->get_error_message() );
	return; // degrade gracefully
}
  • Always check is_wp_error() on anything that can return one (wp_remote_*, wp_insert_post, wp_update_user, etc).
  • Use a prefixed error code so you can trace it in logs.
  • Never wp_die() or exit on the front end for a recoverable problem.

4. Wrap risky operations in try/catch

For code that throws exceptions (3rd party libs, file/JSON/DB work).

1
2
3
4
5
6
7
8
try {
	$client = new \Vendor\Client( MYPLUGIN_API_KEY );
	$result = $client->fetch();
} catch ( \Throwable $e ) {
	error_log( 'myplugin: ' . $e->getMessage() );
	set_transient( 'myplugin_error', $e->getMessage(), HOUR_IN_SECONDS );
	return;
}
  • Catch \Throwable, not \Exception. It also catches PHP 7+ Error (e.g. TypeError, missing method).
  • Log it, don’t swallow it silently.
  • A parse error in the file itself can’t be caught. Lint before deploy: php -l file.php.

5. Display safe admin notices

Notices are how failures surface to the admin instead of the visitor.

1
2
3
4
5
6
7
8
9
function myplugin_missing_deps_notice() {
	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}
	printf(
		'<div class="notice notice-error"><p>%s</p></div>',
		esc_html__( 'My Plugin needs WooCommerce active. Features are disabled.', 'myplugin' )
	);
}
  • Check capability so only admins see it.
  • Escape everything: esc_html(), esc_url(). Never print raw exception messages from user input.
  • Never show stack traces, file paths, keys or API responses in a notice.
  • Show the notice only on relevant screens if it’s noisy (get_current_screen()).
  • Optional: deactivate_plugins( plugin_basename( __FILE__ ) ) on a fatal dependency miss. Do it on admin_init only.

6. Test it

  1. Deactivate each dependency one at a time. Site should load, admin notice should show.
  2. Turn on WP_DEBUG and WP_DEBUG_LOG. No warnings or notices.
  3. Break the API on purpose (wrong URL/key). Front end should keep working.
  4. Run php -l on every changed file.

Related: WordPress Debug Errors

This post is licensed under CC BY 4.0 by the author.