WordPress-Coding-Standards: Newline required after opening brace triggered for allowed loop syntax
The WordPress coding standards state:
You are free to use the alternative syntax for control structures (e.g. if/endif, while/endwhile)—especially in your templates where PHP code is embedded within HTML, for instance:
<?php if ( have_posts() ) : ?>
<div class="hfeed">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
<!-- ... -->
</article>
<?php endwhile; ?>
</div>
<?php endif; ?>
The current version of the standard does not allow this though. Newline required after opening brace is thrown for the following code snippets:
<?php while ( have_posts() ) : the_post(); ?><?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Running phpcbf on these two examples will produce the following, respectively:
<?php
while ( have_posts() ) :
the_post();
?>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 18 (15 by maintainers)
The Loop is a special thing in WordPress, I think exceptions should be made for The Loop
A rudimentary search here on GitHub for
while ( have_posts() ) : the_post();in PHP files resulted in 68 of the 100 results I looked at (the first 10 pages) to use that snippet on a single line.Here’s ~10 codex pages that reference that same single line snippet
https://codex.wordpress.org/The_Loop https://codex.wordpress.org/The_Loop_in_Action https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Function_Reference/have_posts https://codex.wordpress.org/Function_Reference/wp_reset_query https://codex.wordpress.org/Pagination https://codex.wordpress.org/Function_Reference/rewind_posts
And not just in English: https://codex.wordpress.org/fr:La_Boucle https://codex.wordpress.org/it:Il_Loop https://codex.wordpress.org/pt-br:O_Loop https://codex.wordpress.org/zh-cn:循环
It pretty much is a defacto standard IMHO and I would favor an exception being added for these to be honest.
I’m fine with updating the handbook and codex, I just wanted to make sure that we’re all on the same page with regard to the fact that this has basically been the de facto standard up to now. Personally, I think that one statement per line is far more readable; it’s easy to miss the second statement. So I’m happy to just say this isn’t really feasible technically to add an exception for, and move on.
Any progress on this? I agree that an exception should be added. The Loop is such a fundamental part of Wordpress development that an exception makes sense.
I agree with @Rarst here. With HTML templating, you sometimes need to avoid extra whitespace. While you can get around this sniff by
echoing prepared strings, the resulting template is far less readable.I disagree about this being specific to loop or needing an exception for it. In my opinion rule that is specifically about braces formatting is irrelevant to alternative syntax.
Alternative syntax is better suited for being mixed in HTML in some cases, exactly because braces and rigid formatting can get in the way of producing neat meaningful readable template.
I think this rule should just stay away from alternative
ifsyntax altogether, neither the change in current coding standard in regard to it is needed.Yeah, they are allowed to both be on the same line, but if only one of them is on the line, then it has to be by itself without other code.
Which is why “multi-line” is the important word there.
But this isn’t about the tags, it is about brace style. And the handbook does indeed include this example:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#brace-style
The third line (
<?php while ( have_posts() ) : the_post(); ?>) puts code on the line after the “brace.”It could be changed to this:
And that would also pass.
But I think this pattern is common enough that we might want to consider making a special allowance for it.