Currently, the WordPress Coding Standard explicitly forbids the use of the PHP short echo tag (<?=) along with the PHP short tag (<?). This post proposes modifying this rule to allow the use of the short echo tag for single statements.
Prior to PHP 5.4, it was possible to disable the PHP short echo tag (<?=) using the PHP short_open_tag ini directive. This meant that scripts using this tag could not be used in code that must work on different PHP installations, as the content within those tags may be printed instead of executed, which could lead to code exposure. For this reason, the WordPress Coding Standards forbid its use.
Since PHP 5.4, the short echo tag is always available, and changing the short_open_tag directive no longer affects it. WordPress dropped support for versions prior to PHP 5.6 in 2019, and since then raised the minimum supported PHP version to 7.2. Currently, according to WordPress.org stats, the percentage of active WP installs using PHP < 5.4 is 0.4% and the percentage of sites still using WP < 5.2 is 4.0%. Therefore, it is now safe to allow the use of short echo tags.
This tag is useful as it provides a more concise syntax for outputting values in template files. WordPress developers should be allowed to use it. An issue requesting this change is the most liked issue in the WPCS repository, indicating community support.
This proposal is about allowing the use of the short echo tag for single statements, not encouraging its use, so no immediate changes are required. In practice, this means that:
- Existing open patches for Core are not affected as either style is allowed.
- Existing WP Core code and code in official WP themes should not be updated as both styles are permitted. A patch to enforce the use of short echo tags in all possible places will NOT be accepted.
- However, a new official theme could choose to use short echo tags if desired.
The suggestion is to modify the rule titled "No Shorthand PHP Tags" as follows:
New title: No PHP short open tag
Content:
Important: Never use the PHP short open tag (`<?`). Always use the full PHP open tag (`<?php`). Using the PHP short echo tag (`<?=`) is allowed, though short echo tag snippets should only contain a single statement.
Correct:
<?php ... ?>
<?= esc_html( $var ); ?>
Incorrect:
<? ... ?>
If this proposal is accepted, but a project wants to keep the short echo tag forbidden in its own codebase, it can do so by adding the following snippet to its PHPCS configuration after the WordPress standard is included:
<rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound">
<severity>5</severity>
</rule>- Related issue in the WPCS repository: WordPress/WordPress-Coding-Standards#1642
#codingstandards, #php, #wpcs
Hi @rodrigoprimo, I've had a read through this. Here are my notes:
short_open_tagdirective." => "... using the PHPshort_open_tagini directive.";before the close tag./cc @GaryJones @dingo-d