Back to Rules
PHP

WordPress WooCommerce PHP Best Practices: Hooks, Standards & Security Guide

Claude Directory November 29, 2025
3 copies 6 downloads

Optimize your WordPress and WooCommerce development with rewritten PHP coding rules emphasizing hooks, strict typing, security, and modular extensibility for high-performance e-commerce sites.

Rule Content
## Do
- **Employ hooks for extensions**: Integrate functionality via actions and filters instead of altering core files.
  *Example*: `add_action('woocommerce_before_add_to_cart_form', 'custom_cart_message');`

- **Adopt strict typing and modern PHP**: Declare `strict_types=1` and leverage PHP 7.4+ features like typed properties.
  *Example*:
  ```php
declare(strict_types=1);
class MyClass {
    public string $property;
}
```

- **Use WP/WC core functions and APIs**: Rely on built-ins like `wc_get_product()` for products or `wpdb->prepare()` for queries.
  *Example*: `$product = wc_get_product($post_id);`

- **Implement security essentials**: Apply nonces, sanitization, and validation with WP functions; use transients for caching.
  *Example*: `wp_verify_nonce($_POST['nonce'], 'my_action');`

- **Manage assets properly**: Enqueue scripts/styles via `wp_enqueue_script()` and `wp_enqueue_style()`.
  *Example*: `wp_enqueue_script('my-script', get_template_directory_uri() . '/js/script.js', ['jquery'], '1.0', true);`

- **Override WC templates correctly**: Place custom templates in `your-plugin/woocommerce/`.
  *Example*: Copy `single-product.php` to `plugins/my-plugin/woocommerce/single-product.php`.

- **Handle sessions and notices with WC tools**: Store temp data in sessions and display messages via notices.
  *Example*: `WC()->session->set('key', 'value'); wc_add_notice('Success!', 'success');`

## Don't
- **Modify core files directly**: Avoid editing WP or WC core to prevent update issues; use hooks instead.
  *Example (Don't)*: Hacking `wp-content/plugins/woocommerce/woocommerce.php`.

- **Skip database security**: Never use raw SQL without preparation; always use `wpdb->prepare()`.
  *Example (Don't)*: `$wpdb->query("SELECT * FROM table WHERE id = $id");`

- **Ignore naming conventions**: Don't use camelCase for directories; stick to lowercase-hyphen format.
  *Example (Don't)*: `wp-content/plugins/myPlugin` → Use `my-plugin`.

- **Overlook error handling**: Avoid unhandled exceptions; use try-catch and WP logging.
  *Example (Don't)*: Raw `file_get_contents()` without checks → Use `try { ... } catch (Exception $e) { wc_get_logger()->error($e->getMessage()); }`.

- **Reinvent WC features**: Don't query posts manually for products; use WC CRUD classes.
  *Example (Don't)*: `get_post($id)` → Use `wc_get_product($id)`.

- **Forget compatibility checks**: Don't assume WC is active; verify with `if (class_exists('WooCommerce'))`.
  *Example (Don't)*: Unconditional WC function calls without `function_exists()` or class checks.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory