WordPress PHP Best Practices: Essential Coding Rules and Standards Guide
Optimize your WordPress PHP development with rewritten rules emphasizing security, modularity, hooks, and core APIs. Features Do/Don't formats with practical code examples for immediate use.
**Do:**
- Adopt WordPress coding standards and PHP 7.4+ features like strict typing for robust code.
*Example:*
```php
<?php
declare(strict_types=1);
function get_user_data(int $user_id): ?array {
global $wpdb;
return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->users} WHERE ID = %d", $user_id), ARRAY_A);
}
```
**Don't:**
- Ignore strict typing or use loose database queries without preparation.
*Example (Avoid):*
```php
<?php
$result = $wpdb->get_row("SELECT * FROM users WHERE ID = $user_id"); // Vulnerable to SQL injection
```
**Do:**
- Leverage hooks (actions/filters) for extensions and enqueue assets properly.
*Example:*
```php
add_action('wp_enqueue_scripts', function() {
wp_enqueue_style('my-theme-style', get_template_directory_uri() . '/style.css');
});
```
**Don't:**
- Directly edit core files or hardcode scripts/styles.
*Example (Avoid):*
```php
echo '<script>alert("hello");</script>'; // Blocks rendering, insecure
```
**Do:**
- Sanitize inputs, verify nonces, and use wpdb with prepare() for DB ops.
*Example:*
```php
if (wp_verify_nonce($_POST['nonce'], 'my_action')) {
$data = sanitize_text_field($_POST['data']);
$wpdb->insert('my_table', ['value' => $data]);
}
```
**Don't:**
- Skip validation or use raw SQL without escaping.
*Example (Avoid):*
```php
$wpdb->query("INSERT INTO my_table (value) VALUES ('$_POST[data]")"); // Unsafe
```
**Do:**
- Structure files with lowercase hyphens, use OOP for modularity, and transients for caching.
*Example:*
```php
// In wp-content/plugins/my-plugin/my-module.php
class MyModule {
public function get_cached_data(string $key): mixed {
return get_transient($key);
}
}
```
**Don't:**
- Duplicate code or use unclear naming like single letters.
*Example (Avoid):*
```php
function x($a) { return $a * 2; } // Unclear
```
**Do:**
- Handle errors with try-catch, WP debug logging, and wp_cron for tasks; internationalize with i18n.
*Example:*
```php
try {
// risky operation
} catch (Exception $e) {
error_log('Error: ' . $e->getMessage());
}
__('My Text', 'my-plugin');
```
**Don't:**
- Rely on direct cron or neglect localization.
*Example (Avoid):*
```php
echo 'My Text'; // Not translatableComments
More Rules
View allGLM-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.
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.
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.
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.
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.
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.