Skip to content

Latest commit

 

History

History
142 lines (109 loc) · 2.67 KB

File metadata and controls

142 lines (109 loc) · 2.67 KB

WordPress Coding Standards (WPCS)

The Problem

Not following coding standards creates:

  • Inconsistent code - Different styles everywhere
  • Hard to read - Mixed indentation, spacing
  • Team friction - Everyone codes differently
  • Review difficulties - Can't focus on logic

Bad Practice

// Mixed indentation, no spaces
function processData($data){
  if($data){
      foreach($data as $item){
        echo $item;
      }
  }
}

// CamelCase (wrong for WordPress)
function getUserPosts($userId) {
    $postData = [];
}

Good Practice

// Tabs, proper spacing
function process_data( array $data ): void {
	if ( $data ) {
		foreach ( $data as $item ) {
			echo esc_html( $item );
		}
	}
}

// snake_case (WordPress standard)
function get_user_posts( int $user_id ): array {
	$post_data = [];
}

Key WordPress Standards

Indentation

  • Use TABS for indentation
  • Use SPACES for alignment

Naming

  • snake_case for functions and variables
  • PascalCase for classes
  • SCREAMING_SNAKE_CASE for constants

Spacing

  • Space after if, for, foreach, while
  • Space around operators: $a + $b
  • Space after commas: function( $a, $b )
  • No space inside parentheses: if ( $condition )

Braces

  • Opening brace on same line
  • Closing brace on new line
  • Always use braces, even for single statements
// Good
if ( $condition ) {
	do_something();
}

// Bad
if ($condition) do_something();

PHP Tags

  • Always use full tags: <?php ?>
  • Never use short tags: <? ?> or <?= ?>

One Statement Per Line

// Bad
$a = 1; $b = 2; $c = 3;

// Good
$a = 1;
$b = 2;
$c = 3;

Tools

PHP_CodeSniffer

# Install
composer require --dev squizlabs/php_codesniffer

# Install WordPress standards
composer require --dev wp-coding-standards/wpcs

# Check files
phpcs --standard=WordPress file.php

# Auto-fix
phpcbf --standard=WordPress file.php

IDE Integration

  • PHPStorm: Settings → PHP → Quality Tools → PHP_CodeSniffer
  • VS Code: Install "phpcs" extension
  • Sublime: Install "Phpcs" package

Key Takeaways

Use tabs for indentation
Use snake_case for functions/variables
Space after keywords
Always use full PHP tags
Use WordPress functions
Follow WPCS for consistency

❌ Don't mix indentation styles
❌ Don't use camelCase
❌ Don't skip braces
❌ Don't use short tags

The Bottom Line

Consistent code is easier to read, review, and maintain.

Follow WordPress Coding Standards for:

  • Team consistency
  • Easier code reviews
  • Better collaboration
  • Professional quality

Install phpcs and run it on every commit!