### JavaScript Switch Statement Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Provides an example of a switch statement in JavaScript, demonstrating how to handle multiple cases with a single block and use the 'default' case. It also shows how to avoid returning values directly from case blocks. ```javascript switch ( event.keyCode ) { // ENTER and SPACE both trigger x() case $.ui.keyCode.ENTER: case $.ui.keyCode.SPACE: x(); break; case $.ui.keyCode.ESCAPE: y(); break; default: z(); } function getKeyCode( keyCode ) { var result; switch ( event.keyCode ) { case $.ui.keyCode.ENTER: case $.ui.keyCode.SPACE: result = 'commit'; break; case $.ui.keyCode.ESCAPE: result = 'exit'; break; default: result = 'default'; } return result; } ``` -------------------------------- ### JavaScript Array Initialization Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Shows the preferred method for creating arrays in JavaScript using the shorthand `[]` notation. It also demonstrates how to initialize an array with values during its creation. ```javascript var myArray = []; var myArray = [ 1, 'WordPress', 2, 'Blog' ]; ``` -------------------------------- ### Media Query Example for Screen Sizes Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css This example demonstrates how to use media queries to apply specific styles based on screen dimensions. It's generally advised to group media queries at the bottom of a stylesheet, with an exception for wp-admin.css. ```css @media all and (max-width: 699px) and (min-width: 520px) { /* Your selectors */ } ``` -------------------------------- ### JavaScript Good Spacing Examples Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Provides examples of well-formatted JavaScript code, including conditional statements, loops, try-catch blocks, and method chaining. It emphasizes consistent indentation and spacing for improved readability. ```javascript var i; if ( condition ) { doSomething( 'with a string' ); } else if ( otherCondition ) { otherThing( { key: value, otherKey: otherValue } ); } else { somethingElse( true ); } // Unlike jQuery, WordPress prefers a space after the ! negation operator. // This is also done to conform to our PHP standards. while ( ! condition ) { iterating++; } for ( i = 0; i < 100; i++ ) { object[ array[ i ] ] = someFn( i ); $( '.container' ).val( array[ i ] ); } try { // Expressions } catch ( e ) { // Expressions } ``` -------------------------------- ### Markdown Inline Code Source: https://developer.wordpress.org/coding-standards/styleguide Provides an example of how to format inline code snippets within Markdown text using single backticks. ```markdown This is `inline code` wrapped with backticks ``` -------------------------------- ### CSS Property and Value Formatting Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Provides examples of correctly formatted CSS properties and values, including lowercase for most properties, hex codes for colors, and using shorthand where appropriate. It contrasts this with incorrect formatting. ```css #selector-1 { background: #fff; display: block; margin: 0; margin-left: 20px; } ``` -------------------------------- ### Markdown Fenced Code Blocks (Markdown) Source: https://developer.wordpress.org/coding-standards/styleguide Illustrates how to include Markdown syntax itself within fenced code blocks for documentation or examples. ```markdown This is _italic text_. This is **bold text**. ``` -------------------------------- ### Markdown Fenced Code Blocks (PHP) Source: https://developer.wordpress.org/coding-standards/styleguide Provides an example of how to represent PHP code within Markdown using fenced code blocks for clear presentation. ```php $array = array( "foo" => "bar", "bar" => "foo", ); ``` -------------------------------- ### Changelog Entries with @since Tags Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Provides examples of how to use multiple @since tags to document significant changes made to a function, hook, class, or method across different versions. ```php * @since 3.0.0 * @since 3.8.0 Added the `post__in` argument. * @since 4.1.0 The `$force` parameter is now optional. ``` -------------------------------- ### JavaScript Comments Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Demonstrates how to use multi-line and inline comments in JavaScript according to coding standards. Comments should precede the code they explain and follow specific formatting rules. JSDoc comments use `/**` for multi-line documentation. ```javascript someStatement(); // Explanation of something complex on the next line $( 'p' ).doSomething(); // This is a comment that is long enough to warrant being stretched // over the span of multiple lines. function foo( types, selector, data, fn, /* INTERNAL */ one ) { // Do stuff } ``` -------------------------------- ### Markdown Tables Source: https://developer.wordpress.org/coding-standards/styleguide Illustrates the Markdown syntax for creating tables with headers and rows, separated by pipes and hyphens. ```markdown | A | B | | ----- | ----- | | Alpha | Bravo | ``` -------------------------------- ### Documenting Constants in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Standards for documenting constants using DocBlocks. Similar to properties, it includes summary, @since tag format, and @var tag usage. The code example also shows the constant declaration. ```php /** * Summary. * * @since x.x.x * @var type $var Description. */ const NAME = value; ``` -------------------------------- ### Multi-line Inline Comments in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Formatting for multi-line inline comments. These comments start with `/*` and must not begin with `/**` to avoid being mistaken for DocBlocks. Each line within the comment block typically starts with an asterisk. ```php /* * This is a comment that is long enough to warrant being stretched over * the span of multiple lines. You'll notice this follows basically * the same format as the PHPDoc wrapping and comment block style. */ ``` -------------------------------- ### CSS Structure and Formatting Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Demonstrates the correct indentation and spacing for CSS selectors, properties, and values according to WordPress coding standards. It shows how to structure sections and blocks for improved readability. ```css #selector-1, #selector-2, #selector-3 { background: #fff; color: #000; } ``` -------------------------------- ### Markdown Emphasis (Italics, Bold, Strikethrough) Source: https://developer.wordpress.org/coding-standards/styleguide Shows how to apply emphasis to text in Markdown using asterisks and underscores for italics, bold, and strikethrough. ```markdown This is _italic text_. This is **bold text**. This is ~~strikethrough~~ text. ``` -------------------------------- ### Markdown Lists (Unordered and Ordered) Source: https://developer.wordpress.org/coding-standards/styleguide Demonstrates the Markdown syntax for creating both unordered lists using hyphens and ordered lists using numbered items followed by periods. ```markdown - List - List - List - List 1. One 2. Two 3. Three ``` -------------------------------- ### Single-line Inline Comments in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Formatting for single-line inline comments within methods and functions. These comments start with `//`. ```php // Allow plugins to filter an array. ``` -------------------------------- ### Markdown Links Source: https://developer.wordpress.org/coding-standards/styleguide Illustrates the syntax for creating hyperlinks in Markdown, where the link text is enclosed in square brackets and the URL in parentheses. ```markdown [WordPress](https://wordpress.org/) ``` -------------------------------- ### CSS Vendor Prefixes Ordering (Autoprefixer Output) Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Shows the correct order for vendor prefixes in CSS, from longest to shortest, followed by the unprefixed version. This example assumes the use of a tool like Autoprefixer. ```css .sample-output { -webkit-box-shadow: inset 0 0 1px 1px #eee; -moz-box-shadow: inset 0 0 1px 1px #eee; box-shadow: inset 0 0 1px 1px #eee; } ``` -------------------------------- ### Markdown Blockquotes Source: https://developer.wordpress.org/coding-standards/styleguide Explains how to create blockquotes in Markdown using the '>' symbol. Nested blockquotes can be achieved with '>>'. ```markdown > Blockquote >> Indented Blockquote ``` -------------------------------- ### Markdown Headings Source: https://developer.wordpress.org/coding-standards/styleguide Demonstrates the syntax for creating HTML headings (h1-h6) using Markdown. Headings h1 through h4 are automatically added to the Table of Contents. ```markdown # Heading h1 ## Heading h2 ### Heading h3 #### Heading h4 ##### Heading h5 ###### Heading h6 ``` -------------------------------- ### Markdown Fenced Code Blocks (Javascript) Source: https://developer.wordpress.org/coding-standards/styleguide Demonstrates how to use fenced code blocks in Markdown to display Javascript code, specifying the language for syntax highlighting. ```javascript var foo = function (bar) { return bar++; }; console.log(foo(5)); ``` -------------------------------- ### Markdown Fenced Code Blocks (CSS) Source: https://developer.wordpress.org/coding-standards/styleguide Illustrates how to format CSS code within Markdown using fenced code blocks for proper display and syntax highlighting. ```css foo { padding: 5px; margin-right: 3px; } .bar { background-color: #f00; } ``` -------------------------------- ### CSS Property Ordering: Alphabetical Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Presents an alternative method for ordering CSS properties alphabetically. This approach, used by some teams, aims for simplicity and consistency. ```css #overlay { background: #fff; color: #777; padding: 10px; position: absolute; z-index: 1; } ``` -------------------------------- ### CSS Selector Naming and Usage Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Illustrates the correct way to name CSS selectors using lowercase, hyphens for separation, and human-readable names. It also shows proper attribute selector usage and avoiding over-qualified selectors. ```css #comment-form { margin: 1em 0; } input[type="text"] { line-height: 1.1; } ``` -------------------------------- ### Aligning DocBlock Comments in JavaScript Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript This example demonstrates how to align comments within a JSDoc block for improved readability. It shows proper spacing for parameters with varying type and name lengths, ensuring the descriptions align vertically. ```javascript /** * @param {very_long_type} name Description. * @param {type} very_long_name Description. */ ``` -------------------------------- ### CSS Property Ordering: Logical Grouping Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Demonstrates logical grouping of CSS properties for better readability and organization. Properties are grouped by meaning (e.g., display, positioning, box model, colors, typography) and ordered strategically within these groups. ```css #overlay { position: absolute; z-index: 1; padding: 10px; background: #fff; color: #777; } ``` -------------------------------- ### CSS Value Formatting: Correct Examples Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Illustrates correct formatting for various CSS values, including quotes, zero values, font weights, line heights, and multi-part values. Adheres to WordPress CSS standards for consistency. ```css .class { /* Correct usage of quotes */ background-image: url(images/bg.png); font-family: "Helvetica Neue", sans-serif; font-weight: 700; } .class { /* Correct usage of zero values */ font-family: Georgia, serif; line-height: 1.4; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.5), 0 1px 0 #fff; } .class { /* Correct usage of short and lengthier multi-part values */ font-family: Consolas, Monaco, monospace; transition-property: opacity, background, color; box-shadow: 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, 0.8); } ``` -------------------------------- ### Backbone Class with Initialize Function Documentation (JavaScript) Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript Demonstrates the correct JSDoc formatting for Backbone.js classes that include an 'initialize' function. It specifies the use of '@lends' for class definitions and detailed JSDoc tags for documenting the constructor, parameters, and other class metadata. ```javascript Class = Parent.extend( /** @lends namespace.Class.prototype */{ /** * Summary. (use period) * * Description. (use period) * * @since x.x.x * @deprecated x.x.x Use new_function_name() instead. * @access private * * @constructs namespace.Class * @augments Parent * @mixes mixin * * @alias realName * @memberof namespace * * @see Function/class relied on * @link URL * @fires Class#eventName * * @param {Object} attributes The model's attributes. * @param {type} attributes.key One of the model's attributes. * @param {Object} [options] The model's options. * @param {type} options.key One of the model's options. */ initialize: function() { //Do stuff. } } ); ``` -------------------------------- ### Documenting Hooks (Actions and Filters) in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Standards for documenting action and filter hooks. This includes summary, optional description, @ignore, @since, and @param tags. It specifies how to document array parameters and notes that @return is not used. ```php /** * Summary. * * Description. * * @since x.x.x * * @param type $var Description. * @param array $args { * Short description about this hash. * * @type type $var Description. * @type type $var Description. * } * @param type $var Description. */ ``` -------------------------------- ### PHP DocBlock for Sub-Classes Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php DocBlock format for documenting sub-classes in PHP, including a summary, description, versioning, and an @see tag to reference the super class. ```php /** * Summary. * * Description. * * @since x.x.x * * @see Super_Class */ ``` -------------------------------- ### Documenting Required and Included Files in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Guidelines for documenting files that are required or included using a summary DocBlock. This can optionally apply to inline `get_template_part()` calls for clarity. ```php /** * Summary. */ require_once( ABSPATH . WPINC . '/filename.php' ); ``` -------------------------------- ### Indented Code Sample in DocBlock Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Shows how to include a code sample within a DocBlock by indenting every line with 4 spaces. Blank lines within the code sample must also be indented. ```php * Description including a code sample: * * $status = array( * 'draft' => __( 'Draft' ), * 'pending' => __( 'Pending Review' ), * 'private' => __( 'Private' ), * 'publish' => __( 'Published' ) * ); * * The description continues on ... ``` -------------------------------- ### CSS Inline Comment Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css This example shows the correct way to add inline comments within CSS. Inline comments should directly follow the code they describe and explain specific choices, like the use of '!important'. ```css /* This is a comment about this selector */ .another-selector { position: absolute; top: 0 !important; /* I should explain why this is so !important */ } ``` -------------------------------- ### JavaScript Object Creation and Property Access Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Demonstrates the preferred methods for creating JavaScript objects using literals and constructors, and accessing their properties via dot notation or bracket notation when keys are variables or invalid identifiers. ```javascript var myObj = {}; ``` ```javascript var myObj = new ConstructorMethod(); ``` ```javascript prop = object.propertyName; prop = object[ variableKey ]; prop = object['key-with-hyphens']; ``` -------------------------------- ### Object Structure: One Class per File in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Illustrates the WordPress coding standard of having only one primary object structure (class, interface, trait, or enum) defined per PHP file. Shows an incorrect example with multiple classes in one file and correct examples where each class resides in its own dedicated file. ```php // Incorrect: file class-example-class.php. class Example_Class {} class Example_Class_Extended {} ``` ```php // Correct: file class-example-class.php. class Example_Class {} ``` ```php // Correct: file class-example-class-extended.php. class Example_Class_Extended {} ``` -------------------------------- ### Documenting Class Properties in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Standards for documenting class properties using DocBlocks. Includes summary, @since tag format, and @var tag usage. The description for @var may be omitted. ```php /** * Summary. * * @since x.x.x * @var type $var Description. */ ``` -------------------------------- ### JavaScript String Literal Example Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Illustrates the use of single-quotes for string literals in JavaScript. When a string itself contains a single quote, it must be escaped with a backslash. ```javascript var myStr = 'strings should be contained in single quotes'; // Escape single quotes within strings: 'Note the backslash before the \'single quotes\'' ``` -------------------------------- ### Documenting Duplicate Hooks in PHP Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Guidelines for documenting duplicate hooks. For subsequent occurrences, a single-line comment referencing the documented version is used, either for actions or filters. ```php /** This action is documented in path/to/filename.php */ ``` ```php /** This filter is documented in path/to/filename.php */ ``` -------------------------------- ### Unordered List in DocBlock Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Demonstrates how to create an unordered list within a DocBlock using hyphens. A blank line is required before and after the list. ```php * Description which includes an unordered list: * * - This is item 1. * - This is item 2. * - This is item 3. * * The description continues on ... ``` -------------------------------- ### Markdown Horizontal Rules Source: https://developer.wordpress.org/coding-standards/styleguide Shows the simple Markdown syntax for creating a horizontal rule using three hyphens. ```markdown --- ``` -------------------------------- ### Correct Object Instantiation Syntax in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Demonstrates the correct syntax for instantiating new objects in PHP. It requires the use of parentheses after the class name, even when no arguments are passed, and prohibits spaces between the class name and the opening parenthesis. ```php // Correct. $foo = new Foo(); $anonymous_class = new class( $parameter ) { ... }; $instance = new static(); ``` -------------------------------- ### Ordered List in DocBlock Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Illustrates the correct way to format an ordered list within a DocBlock using numbers. Ensure a blank line precedes and follows the list. ```php * Description which includes an ordered list: * * 1. This is item 1. * 2. This is item 2. * 3. This is item 3. * * The description continues on ... ``` -------------------------------- ### PHP DocBlock for Classes Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Standard DocBlock format for PHP classes, including a summary and description of the class's purpose, along with versioning information using the @since tag. ```php /** * Summary. * * Description. * * @since x.x.x */ ``` -------------------------------- ### Multi-line Inline Comment Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript A multi-line comment for more extensive explanations. It starts with `/*` and ends with `*/`. Each line within the comment block typically begins with an asterisk `*`. ```javascript /* * This is a comment that is long enough to warrant being stretched over * the span of multiple lines. You'll notice this follows basically * the same format as the JSDoc wrapping and comment block style. */ ``` -------------------------------- ### PHP DocBlock for Array Parameters Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Documenting array parameters in PHP functions using hash notation, specifying types, keys, descriptions, defaults, and accepted values for each array element. This is typically done in the originating function's DocBlock. ```php /** * Summary. * * Description. * * @since x.x.x * * @param type $var Description. * @param array $args { * Optional. An array of arguments. * * @type type $key Description. Default 'value'. Accepts 'value', 'value'. * (aligned with Description, if wraps to a new line) * @type type $key Description. * } * @param type $var Description. * @return type Description. */ ``` -------------------------------- ### Single Line Inline Comment Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript A simple single-line comment used for brief explanations within code. Starts with double slashes `//` and extends to the end of the line. ```javascript // Extract the array values. ``` -------------------------------- ### Underscore.js Chaining for Object Transformation Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Shows how to use Underscore.js's chaining capabilities with JavaScript objects for efficient and readable data transformations. It demonstrates methods like keys(), map(), and value(). ```javascript var obj = { first: 'thing 1', second: 'thing 2', third: 'lox' }; var arr = _.chain( obj ) .keys() .map( function ( key ) { return key + ' comes ' + obj[ key ]; } ) // Exit the chain .value(); // arr === [ 'first comes thing 1', 'second comes thing 2', 'third comes lox' ] ``` -------------------------------- ### Markdown Fenced Code Blocks (HTML) Source: https://developer.wordpress.org/coding-standards/styleguide Shows the Markdown syntax for embedding HTML code within fenced code blocks, ensuring correct rendering and highlighting. ```html Example ``` -------------------------------- ### PHP DocBlock for Functions and Class Methods Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Standard DocBlock format for PHP functions and class methods, including common tags for summary, description, versioning, access control, references, globals, parameters, and return values. ```php /** * Summary. * * Description. * * @since x.x.x * * @see Function/method/class relied on * @link URL * @global type $varname Description. * @global type $varname Description. * * @param type $var Description. * @param type $var Optional. Description. Default. * @return type Description. */ ``` -------------------------------- ### Markdown Fenced Code Blocks (JSON) Source: https://developer.wordpress.org/coding-standards/styleguide Shows the correct Markdown syntax for embedding JSON data within fenced code blocks, enabling syntax highlighting. ```json { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [], "spouse": null } ``` -------------------------------- ### Using $wpdb->prepare() for SQL Queries in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php When executing SQL queries in PHP with WordPress, use `$wpdb->prepare()` to safely handle data escaping and quoting. This method uses `sprintf()`-style placeholders (%d for integers, %f for floats, %s for strings, %i for identifiers) and prevents SQL injection vulnerabilities. ```php $var = "dangerous'"; // Raw data that may or may not need to be escaped. $id = some_foo_number(); // Data we expect to be an integer, but we're not certain. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) ); ``` -------------------------------- ### PHP DocBlock for Deprecated Functions Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Documenting deprecated PHP functions using the @deprecated tag, specifying the version of deprecation and suggesting a replacement function. Includes @see tag for linking to the replacement. ```php /** * Summary. * * Description. * * @since x.x.x * @deprecated x.x.x Use new_function_name() * @see new_function_name() * * @param type $var Optional. Description. * @param type $var Description. * @return type Description. */ ``` -------------------------------- ### Ignoring Code Blocks with JSHint Directives Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Demonstrates how to exclude specific sections of JavaScript code from JSHint analysis using 'jshint ignore:start' and 'jshint ignore:end' directive comments. This is useful for third-party code or minified scripts. ```javascript /* jshint ignore:start */ if ( typeof jQuery.fn.hoverIntent === 'undefined' ) { // hoverIntent r6 - Copy of wp-includes/js/hoverIntent.min.js (function(a){a.fn.hoverIntent=............... } /* jshint ignore:end */ ``` -------------------------------- ### JavaScript Object and Array Declarations Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Demonstrates preferred and acceptable ways to declare JavaScript objects and arrays, emphasizing line length and comma placement. It highlights the importance of one property/member per line for longer declarations and the conditional quoting of property names. ```javascript // Preferred var obj = { ready: 9, when: 4, 'you are': 15, }; var arr = [ 9, 4, 15, ]; // Acceptable for small objects and arrays var obj = { ready: 9, when: 4, 'you are': 15 }; var arr = [ 9, 4, 15 ]; // Bad var obj = { ready: 9, when: 4, 'you are': 15 }; var arr = [ 9, 4, 15 ]; ``` -------------------------------- ### Invalid Namespace Declarations - PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Shows examples of incorrect namespace declarations in PHP that violate WordPress coding standards. This includes using curly brace syntax for namespaces and explicit global namespace declarations. ```php // Incorrect: namespace declaration using curly brace syntax. namespace Foo { // Code. } // Incorrect: namespace declaration for the global namespace. namespace { // Code. } ``` -------------------------------- ### Correct Trait Use Statements Formatting in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Demonstrates the correct formatting for trait 'use' statements in PHP classes, including spacing, grouping, and aliasing. It adheres to specific blank line requirements before and after 'use' statements, and proper indentation for complex aliasing. ```php // Correct. class Foo { use Bar_Trait; use Foo_Trait, Bazinga_Trait { Bar_Trait::method_name insteadof Bar_Trait; Bazinga_Trait::method_name as bazinga_method; } use Loopy_Trait { eat as protected; } public $baz = true; ... } ``` -------------------------------- ### CSS Value Formatting: Incorrect Examples Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/css Highlights common mistakes in CSS value formatting, such as missing spaces or semicolons, unnecessary units on zero values, incorrect font weight notation, and improper handling of multi-part values. ```css .class { /* Avoid missing space and semicolon */ background:#fff } .class { /* Avoid adding a unit on a zero value */ margin: 0px 0px 20px 0px; } .class { font-family: Times New Roman, serif; /* Quote font names when required */ font-weight: bold; /* Avoid named font weights */ line-height: 1.4em; /* Avoid adding a unit for line height */ } .class { /* Incorrect usage of multi-part values */ text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5), 0 1px 0 #fff; box-shadow: 0 1px 0 rgba(0, 0, 0, 0.5), 0 1px 0 rgba(0,0,0,0.5); } ``` -------------------------------- ### require/include Statements - PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Use `require[_once]` or `include[_once]` without parentheses around the file path. Prefer `require[_once]` over `include[_once]` for unconditional includes to prevent execution continuation upon file not found errors. Ensure a single space between the keyword and the path. ```PHP // Correct. require_once ABSPATH . 'file-name.php'; // Incorrect. include_once ( ABSPATH . 'file-name.php' ); require_once __DIR__ . '/file-name.php'; ``` -------------------------------- ### JSDoc File Header Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript Provides an overview of the JavaScript file, including a summary, detailed description, author, and version. It can also include JSHint inline configuration options at the end. ```javascript /** * Summary. (use period) * * Description. (use period) * * @link URL * @file This files defines the MyClass class. * @author AuthorName. * @since x.x.x */ /** jshint {inline configuration here} */ ``` -------------------------------- ### Backbone Class Without Initialize Function Documentation (JavaScript) Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/javascript Illustrates how to document a Backbone.js class that does not have an 'initialize' function. It utilizes the '@inheritDoc' tag to inherit documentation, addressing a known JSDoc bug. This method ensures proper documentation even without an explicit initialize function. ```javascript /** * Summary. (use period) * * Description. (use period) * * @since x.x.x * @deprecated x.x.x Use new_function_name() instead. * @access private * * @constructs namespace.Class * @memberOf namespace * @augments Parent * @mixes mixin * @inheritDoc * * @alias realName * @memberof namespace * * @see Function/class relied on * @link URL */ Class = Parent.extend( /** @lends namespace.Class.prototype */{ // Functions and properties. } ); ``` -------------------------------- ### Incorrect Import Use Statements in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Highlights common mistakes and incorrect patterns when using import 'use' statements in PHP. This includes issues with whitespace, incorrect ordering of import types, leading backslashes, improper aliasing, and mixing different import types within a single group statement. These examples violate WordPress coding standards. ```php namespace Project_Name\Feature; use const Project_Name\Sub_Feature\CONSTANT_A; // Superfluous whitespace after the "use" and the "const" keywords. use function Project_Name\Sub_Feature\function_a; // Function import after constant import. use \Project_Name\Sub_Feature\Class_C as aliased_class_c; // Leading backslash shouldn't be used, alias doesn't comply with naming conventions. use Project_Name\Sub_Feature\{Class_D, Class_E as Aliased_Class_E} // Extra spaces around the "as" keyword, incorrect whitespace use inside the brace opener and closer. use Vendor\Package\{ function function_a, function function_b, Class_C, const CONSTANT_NAME}; // Combining different types of imports in one use statement, incorrect whitespace use within group use statement. class Foo { // Code. } use const \Project_Name\Sub_Feature\CONSTANT_D as Aliased_constant; // Import after class definition, leading backslash, naming conventions violation. use function Project_Name\Sub_Feature\function_b as Aliased_Function; // Import after class definition, naming conventions violation. // Rest of the code. ``` -------------------------------- ### @since Version Tag in DocBlock Source: https://developer.wordpress.org/coding-standards/inline-documentation-standards/php Demonstrates the correct format for the @since tag to indicate the WordPress version a function, hook, class, or method was introduced. ```php * @since 4.4.0 ``` -------------------------------- ### JavaScript Class Definitions Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript Class constructors and definitions should use UpperCamelCase. This applies to both traditional classes and stateless function components from `@wordpress/element` for consistency and future compatibility. ```javascript class Earth { static addHuman( human ) { Earth.humans.push( human ); } static getHumans() { return Earth.humans; } } Earth.humans = []; ``` -------------------------------- ### PHP Interpolation for Dynamic Hooks Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Demonstrates the recommended way to name dynamic hooks using interpolation with curly braces for variables, ensuring better readability and discoverability. ```php do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); ``` -------------------------------- ### Documenting Global Variables Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/javascript All global variables used within a JavaScript file should be documented at the top using a `/* global variableName:true */` comment. The `:true` suffix indicates the global is defined within the current file; omitting it signifies read-only access. ```javascript /* global passwordStrength:true */ ``` -------------------------------- ### Indentation with Tabs and Spaces in PHP Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php Demonstrates the use of tabs for logical structure indentation and spaces for mid-line alignment in PHP code, especially for associative arrays and switch statements. Follows WordPress coding standards for readability and flexibility. ```php $foo = 'somevalue'; $foo2 = 'somevalue2'; $foo34 = 'somevalue3'; $foo5 = 'somevalue4'; ``` ```php $query = new WP_Query( array( 'ID' => 123 ) ); ``` ```php $args = array( 'post_type' => 'page', 'post_author' => 123, 'post_status' => 'publish', ); $query = new WP_Query( $args ); ``` ```php $my_array = array( 'foo' => 'somevalue', 'foo2' => 'somevalue2', 'foo3' => 'somevalue3', 'foo34' => 'somevalue3', ); ``` ```php switch ( $type ) { case 'foo': some_function(); break; case 'bar': some_function(); break; } ```