--- name: wordpress-plugin-conventions description: Use when writing or modifying a WordPress plugin's PHP code. Baseline structural/security conventions to follow without being told each time. --- # WordPress Plugin Conventions Baseline conventions for first-party WordPress plugin code. Follow these without needing to be told in every brief. ## File structure ``` plugin-slug/ plugin-slug.php — main file: header, constants, bootstrap class includes/ class--*.php — one class per concern, class-based not procedural admin/ — admin-only UI (settings pages, dashboards) assets/{css,js}/ languages/ — .pot/.po/.mo if the plugin is translatable ``` ## Main file skeleton ```php prepare()` for every query with a variable in it — no string- interpolated SQL, ever, no exceptions. ## Database tables If a plugin needs its own tables (not just options), use `dbDelta()`: ```php require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$wpdb->prefix}prefix_thing (...) {$charset};"; dbDelta($sql); ``` Track a `DB_VERSION` constant + a stored option so future plugin updates can detect and re-run `dbDelta()` for schema changes. ## PHP 7.4 compatibility Unless a specific brief says otherwise, target PHP 7.4+ (a large fraction of real WordPress hosting is still on 7.4). Avoid PHP 8-only syntax (named arguments, enums, readonly properties, `match`). If a codebase needs `str_contains`/`str_starts_with`/`str_ends_with` (PHP 8.0+) on an environment that might run 7.4, guard with `function_exists()` polyfills rather than assuming they exist. ## Don't build what WordPress core already gives you Before writing custom code for: cron scheduling (`wp_schedule_event`), REST endpoints (`register_rest_route`), settings UI (`register_setting`/`add_settings_field`/`woocommerce_admin_fields` if WooCommerce context), object caching (`wp_cache_*` functions), HTTP requests (`wp_remote_get`/`wp_remote_post`, not raw `curl`) — check whether a core API already does it. Reinventing these is both wasted effort and a common source of subtle bugs core already solved correctly.