From the course: Upgrading Legacy WordPress Projects: Modernize Workflows and Codebase

Understanding WordPress coding standards

- [Narrator] Writing consistent and well structured code is essential for maintainability, security and collaboration. In this video, we'll explore what WordPress coding standards are, why they matter, and how to enforce them in your projects. Let's get started. WordPress coding standards can help maintain readable and consistent code across projects, improve collaboration among developers, prevent security vulnerabilities by enforcing best practices, and ensure compatibility with WordPress core updates. Following these standards results in better, more maintainable code bases. WordPress coding standards apply to multiple languages. For PHP, formatting, indentation, function naming are a few areas that are covered. For JavaScript, proper variable declarations, structure, and best practices. For CSS, class naming conventions and organization methods. For HTML, we write clean semantic markup and of course, across all languages, standards for inline documentation. Adhering to these standards makes development more structured and efficient and also easier to read. Some key PHP coding rules in WordPress include using four spaces for indentation, placing opening braces on the same line as function definitions and naming functions with all lowercase and underscores between words. These practices enhance readability and can prevent common errors. Some examples of WordPress JavaScript coding standards include using camelCase for variables and function names, avoiding global variables to prevent conflicts, always using strict comparisons. For example, using three equal signs instead of two for comparing two values and ensuring proper spacing and indentation for readability. Consistent JavaScript formatting makes code easier to debug and maintain. WordPress CSS and HTML standards focus on using lowercase and hyphens for CSS class names, indenting with tabs for CSS, unlike PHP and JavaScript, ordering properties logically like positioning, and then box model, and then typography, and then visuals and writing clean semantic HTML for better accessibility. Structured styles and markup improve site performance and usability. To maintain consistency, use automated tools like PHP_CodeSniffer, which checks PHP code for WordPress standards. ESLint ensures JavaScript follows best practices and Stylelint, enforcing CSS formatting rules. Using these tools automates code review and improves development workflow. Our course, "GitHub Repository "and Code Space Configuration" has these tools already installed and configured. To write clean and efficient WordPress code, follow WordPress coding standards consistently. Use linters and formatters to catch errors early, document functions and code blocks for better readability, test and review code before committing diversion control and stay updated with the latest in coding standards. Adopting these habits ensures a professional and maintainable code base. Today we explored WordPress coding standards and how to enforce them using automated tools. If you haven't already, install PHPCS ESLint and Stylelint in your development workflow to maintain clean code.

Contents