Skip to content

PHP tag must be on a line by itself - creates invalid output in HTML attributes #829

@kkmuffme

Description

@kkmuffme

Describe the bug

Opening PHP tag must be on a line by itself
Closing PHP tag must be on a line by itself

Code sample

<span data-foo="<?php
echo "hello";
?>">

Custom ruleset

https://github.com/WordPress/WordPress-Coding-Standards

To reproduce

See WP CI in WordPress/wordpress-develop#8335 https://github.com/WordPress/wordpress-develop/actions/runs/13371451585/job/37340655741?pr=8335

Expected behavior

Putting the PHP opening/closing tags on a separate line when used in attributes means that the attribute will include leading/trailing whitespace in output.
Since browsers know this is a common bug, they tend to ignore it, but it means that e.g. href= includes an invalid URL.
For some attributes it might actually break it or have unintended behavior e.g. pattern=, placeholder=
Besides the fact, that newlines aren't allowed in attributes.

Versions (please complete the following information)

I guess 3.11.3 in WP CI

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions