| name | php-standards |
| description | PHP coding standards for Oh My Brand! theme. WordPress Coding Standards, strict typing, escaping, sanitization, DocBlocks, and security practices. Use when writing PHP functions, classes, or render templates. |
| metadata | {"author":"Wesley Smits","version":"1.0.0"} |
PHP Standards
PHP coding standards and security practices for the Oh My Brand! WordPress FSE theme.
When to Use
- Writing new PHP functions, classes, or methods
- Creating block render templates (
render.php)
- Building helper functions (
helpers.php)
- Working with WordPress hooks and filters
- Handling user input or output
Reference Files
File Header
Every PHP file must include:
<?php
declare(strict_types=1);
See file-structure.php for complete structure.
Naming Conventions
| Type | Convention | Example |
|---|
| Classes | PascalCase | GalleryBlock |
| Functions | snake_case with prefix | omb_get_gallery_images() |
| Methods | snake_case | get_images() |
| Variables | snake_case | $gallery_images |
| Constants | SCREAMING_SNAKE | OMB_VERSION |
| Files | kebab-case | gallery-block.php |
Function Prefix
Use omb_ prefix for theme functions:
function omb_register_blocks(): void { }
function register_blocks(): void { }
Type Declarations
Use type hints for all function parameters and return types:
function format_gallery_images(array $images, int $limit = 10): array {
}
Common Type Patterns
| Type | Usage |
|---|
string | Text values |
int | Integer numbers |
float | Decimal numbers |
bool | Boolean values |
array | Arrays (use PHPDoc for element types) |
?string | Nullable string |
void | No return value |
Output Escaping
All output must be escaped based on context:
| Function | Use Case |
|---|
esc_html() | Text content |
esc_attr() | HTML attributes |
esc_url() | URLs |
wp_kses_post() | Rich HTML content |
wp_json_encode() | JavaScript values |
esc_html__() | Translated text |
esc_attr__() | Translated attributes |
See escaping-examples.php for examples.
Input Sanitization
Sanitize all input data before use:
| Function | Use Case |
|---|
sanitize_text_field() | Text input |
sanitize_textarea_field() | Textarea |
sanitize_email() | Email |
absint() | Integer |
esc_url_raw() | URL for database |
sanitize_file_name() | File name |
sanitize_html_class() | HTML class |
See sanitization-examples.php for examples.
Nonce Verification
Use nonces for form submissions and AJAX:
| Function | Purpose |
|---|
wp_nonce_field() | Add nonce to form |
wp_create_nonce() | Create nonce for AJAX |
wp_verify_nonce() | Verify form nonce |
check_ajax_referer() | Verify AJAX nonce |
See nonce-examples.php for examples.
WordPress Hooks
| Hook Type | Function | Custom Hook |
|---|
| Actions | add_action() | do_action() |
| Filters | add_filter() | apply_filters() |
Common Actions
add_action('init', 'omb_register_blocks');
add_action('wp_enqueue_scripts', 'omb_enqueue_assets');
add_action('after_setup_theme', 'omb_setup_theme');
Hook Priority
add_action('init', 'omb_early_init', 5);
add_action('init', 'omb_normal_init');
add_action('init', 'omb_late_init', 20);
See hooks-examples.php for examples.
Error Handling
Use early returns and guard clauses:
function omb_get_gallery_html(int $gallery_id): string {
if ($gallery_id <= 0) {
return '';
}
$gallery = get_post($gallery_id);
if (!$gallery instanceof WP_Post) {
return '';
}
return omb_render_gallery($gallery);
}
Related Skills
References