Skip to content

Error caused by using is_embed() and is_feed() in is_amp_endpoint() before condition (! $did_parse_query) #4525

@hansschuijff

Description

@hansschuijff

Bug Description

When calling is_amp_endpoints() at an early stage in a plugin, the conditionals is_embed() en is_feed() issue an error notice, saying it can't be used before the query is finished. It doesn't reach the code that is used to test if the function is started too early, so the cause is unclear at first.

Expected Behaviour

The function tests if it is started before the 'parse_query' hook and starts doing_it_wrong saying is_amp_endpoint() it is started to early in the process. This would be the intended behaviour when it is started too early. it would be solved by moving the if(! $did_parse_query) block up so it runs and before testing the template tags. now it doesn't reach that test depending on how early it is called.

Steps to reproduce

  1. Add a if statement with the condition is_amp_embed() at the global level of a plugin
  2. activate the plugin
  3. refresh a page at the website's frontend.
  4. See error in the log (or whoops)

Screenshots

the notice is:
is_embed was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.)

Additional context

  • WordPress version: 5.4
  • Plugin version: 1.5.2
  • PHP version: 7.3.2

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

  • Improve the _doing_it_wrong() message for when is_amp_endpoint() is called before the wp action.
  • Account for additional template conditionals: is_comment_feed(), is_trackback(), is_robots(), and is_favicon().
  • Remove special condition to allow is_amp_endpoint() to not emit _doing_it_wrong() when called before wp when in Reader mode.
  • Prevent emitting _doing_it_wrong() errors when by calling is_embed() or is_feed() before they are able to be called.
  • Prevent emitting duplicated warnings.
  • Remove separate condition for doing redirects to non-AMP when AMP is not available in Reader mode.
  • Remove unnecessary exit logic.
  • Fix ability to access the page_for_posts if designated to be enabled in AMP Reader mode.

Metadata

Metadata

Assignees

Labels

ChangeloggedWhether the issue/PR has been added to release notes.RoutingHandling URLsWS:CoreWork stream for Plugin core

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions