PHP Coding Standardization
In WordPress some part of the codes are inconsistent in their style.For that word press is working gradually to make the code clean and easy to read at a glance and all the user can be benefitted by the consistent style.
We should keep the following points in mind while writing PHP code for wordpress,whether for core programming code,plugins,or themes.
Single and double quotes
We must use the single and double quote wherever it is applicable.For the evaluation of any variables we use double quote otherwise we use single.But never forget to use quote for the string data type.
|echo ‘<a href=”/static/link” title=”Yeah yeah!”>Link name</a>’;
echo “<a href=’$link’ title=’$linktitle’>$linkname</a>”;
We must use indentation inside our code so that our code can be easily traced in case of errors.We must use tab not spaces.
Above one can be done as
We must uses braces for all blocks in accordance with the suitable styles:
Use of elseif not else if
As else if is not compatible wih the colon syantax of if|elseif blocks.So we must use elseif for conditionals.
We must use the _preg functions in order to do regular expressions related work.Its most convinent to use single-quoted strings for regular expressions since, contrary to double –quoted strings,as they have only two metasequences: \’ and \\.
Opening and closing PHP tags
While coding php within or on the same page with HTML we must be careful about opening and closing tags of php.
No Shorthand PHP tags
We should never use shorthand php tags.We must use full php code.
Remove trailing spaces
We must remove trailing whitespace at the end of each line of code.It is preferred to omit the closing PHP tag at the end of a file.If we use the tag we must make sure of removing trailing whitespace.
We must use spaces after every comma and at both side of logical , comparision ,string and assignment operators.
Formatting sql statements
While formatting sql statements we must break the code into several lines and identations must be properly followed.The sql parts like UPDATE CREATE must always be capitalized.
We must avoid touching the database directly.Using database abstraction rather than queries helps to make our code forward – compatible.If we must touch the database , first get in touch with some developers by posting them message to the wp-hackers mailing list.
We must use lowercase letters in variable , action/filter and function names (never camelCase).We must separate words via underscores(let the code be unambigious and self-documenting).
We must use ternary operator if the statement is true not false.
We must write the codes more readable than focusing on its clevarity.
This much for today.I think You are benefitted by this file.