Make WordPress Core

Opened 12 months ago

Last modified 22 hours ago

#64260 reviewing defect (bug)

Default filter `doing_it_wrong_trigger_error` for REST request cause no notice at all.

Reported by: okvee Owned by: westonruter
Priority: normal Milestone: 7.3
Component: REST API Version: 4.4
Severity: normal Keywords: has-patch changes-requested
Cc: Focuses: rest-api

Description

File: wp-includes/rest-api.php inside function rest_api_default_filters() has this code.

if ( wp_is_serving_rest_request() ) {
    // ...
    add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
}

With this filter, all wrong calling code will not showing the notice anywhere including in wp-content/debug.log.

For example:
Function register_rest_route() ( ​https://developer.wordpress.org/reference/functions/register_rest_route/ ). There are many detection to call _doing_it_wrong() to notice developer to know that they are doing it wrong and let them fix it. These notices are useful and some can improve security.

When I call function register_rest_route() without permission_callback argument, it should showing the notice about doing it wrong somewhere such as in debug.log.

But inside function _doing_it_wrong(), your if condition:

if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function_name, $message, $version ) )

calls to filter doing_it_wrong_trigger_error while this filter always return false. This cause function _doing_it_wrong() completely useless.

Please consider change this default filter value or make it at least write the "doing it wrong" notice to wp-content/debug.log file.

Tested with WordPress development version (6.9-RC1-61214).

Attachments (1)

doing-it-wrong-rest.diff​ (535 bytes ) - added by iflairwebtechnologies 12 months ago.
We have checked and created one patch for this issue

Download all attachments as: .zip

Change History (23)

#1 @westonruter
12 months ago

  • Keywords needs-patch added
  • Milestone Awaiting Review → 7.0
  • Version 6.8.3 → 4.4

The code in question was added way back in r34928 (​b392114) when the REST API was first introduced in core. The intention was for the deprecation message to be sent back via the X-WP-DeprecatedFunction response header. But I don't see why it can't also be sent to the error log.

@iflairwebtechnologies
12 months ago

We have checked and created one patch for this issue

This ticket was mentioned in ​PR #10537 on ​WordPress/wordpress-develop by ​ilclaudio.


12 months ago
#2

  • Keywords has-patch added; needs-patch removed

Fixes #64260. The doing_it_wrong_trigger_error filter was preventing all doing_it_wrong notices from being logged during REST requests. This change ensures notices are written to debug.log while still preventing trigger_error() from interfering with REST responses.

Now the error is filterd but in the debug.log file you can see a meeesage like this:

[21-Nov-2025 14:01:41 UTC] REST API - Doing it wrong: register_rest_route - The REST API route definition for <code>test/v1/example</code> is missing the required <code>permission_callback</code> argument. For REST API routes that are intended to be public, use <code>return_true</code> as the permission callback. (This message was added in version 5.5.0.)

​@ioclaudio commented on ​PR #10537:


12 months ago
#3

It looks like one of the GitHub Actions jobs failed due to a transient network issue (curl error 7 while downloading from packagist.org) and not because of my code changes.
Could someone please re-run the failed job? Thank you!

​@westonruter commented on ​PR #10537:


12 months ago
#4

Could someone please re-run the failed job? Thank you!

Re-run!

​@juanmaguitar commented on ​PR #10537:


9 months ago
#5

@ilclaudio There's some feedback provided in this PR.
Do you think you'll have time to address it ​in time for WP 7.0?

​@ioclaudio commented on ​PR #10537:


9 months ago
#6

Hi @juanmaguitar I have only proposed a patch but I'm not responsible to include it into the official code. I honestly don't know what else to do.

​@juanmaguitar commented on ​PR #10537:


9 months ago
#7

@ilclaudio there's some feedback on the PR provided by @westonruter that still needs to be addressed

why not just remove the doing_it_wrong_trigger_error filter altogether?

A response ​to that comment would be helpful to move forward with this PR

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


8 months ago

#9 @audrasjb
8 months ago

  • Milestone 7.0 → 7.1

As per today's 7.0 pre-RC1 bug scrub:
The PR is still being discussed. Moving to 7.1.

​claudiobat commented on ​PR #10537:


8 months ago
#10

@ilclaudio, first of all thank for proposing this patch for the ticket :)

There's some feedback on the PR provided by @westonruter that still needs to be addressed

why not just remove the doing_it_wrong_trigger_error filter altogether?

A response ​to that comment would be helpful to move forward with this PR

Sorry for the late reply, feedback addressed.

#11 @ioclaudio
6 months ago

Hi all,
the fix has been updated to address all review feedback (moved logging into rest_handle_doing_it_wrong(), added PHP Notice: prefix, included caller location via debug_backtrace()).
​https://github.com/WordPress/wordpress-develop/pull/10537

Last edited 6 months ago by ioclaudio (previous) (diff)

#12 @westonruter
6 months ago

  • Owner set to westonruter
  • Status new → reviewing

​@ioclaudio commented on ​PR #10537:


6 months ago
#13

Thanks for the feedback @westonruter.
Both points addressed in the latest commits and tests are passing.
Sorry for the confusion with my work account @claudiobat: my account for WordPress contributions is @ilclaudio, linked to my WordPress.org profile @ioclaudio.
This is my first contribution attempt, I guess it shows.

#14 @westonruter
6 months ago

  • Keywords changes-requested added

​@ioclaudio commented on ​PR #10537:


5 months ago
#15

Thanks @westonruter, addressed in the latest commits:

  • Added error_reporting() & E_USER_NOTICE gate.
  • Fixed path comparison for Windows and non-standard layouts: wp_normalize_path() on both sides + trailingslashit() to prevent false matches on sibling directories (e.g. wp-content-backup/).
  • Added depth limit of 20 to debug_backtrace().
  • Re-added wp_strip_all_tags(): without it the log contains raw HTML tags from the message strings (e.g. <code>). Happy to drop it again if you prefer.

Examples:

  • Withouth wp_strip_all_tags:"""

[06-Jun-2026 16:44:51 UTC] PHP Notice: register_rest_route (since 5.5.0; The REST API route definition for <code>test/v1/example</code> is missing the required <code>permission_callback</code> argument. For REST API routes that are intended to be public, use <code>return_true</code> as the permission callback.) in /var/www/src/wp-content/plugins/rest-api-test/reast-api-test.php on line 8
"""

  • With wp_strip_all_tags:"""

[06-Jun-2026 16:51:08 UTC] PHP Notice: register_rest_route (since 5.5.0; The REST API route definition for test/v1/example is missing the required permission_callback argument. For REST API routes that are intended to be public, use return_true as the permission callback.) in /var/www/src/wp-content/plugins/rest-api-test/reast-api-test.php on line 8
"""

​@ioclaudio commented on ​PR #10537:


5 months ago
#16

Here's a summary of everything addressed in the latest commits:

  • Extended debug logging to rest_handle_deprecated_function() and rest_handle_deprecated_argument(), matching the pattern in rest_handle_doing_it_wrong()
  • Extracted backtrace logic into _rest_get_debug_backtrace_caller(); a private helper reused by all three handlers, which now also includes the offending function name (e.g. called from MyPlugin->init())
  • Added error_reporting() gate (E_USER_NOTICE / E_USER_DEPRECATED) to mirror native PHP behavior
  • Fixed Windows path comparison: wp_normalize_path() on both sides + trailingslashit() to prevent false matches on sibling directories + strtolower() for case-insensitive comparison
  • Re-added wp_strip_all_tags(), without it, message strings containing <code> tags produce raw HTML in the log

After launching a Claude Code review there are a few things intentionally left as-is, in case a future automated review flags them, this is a summary: "

  • deprecated_hook_trigger_error not suppressed: this is a pre-existing gap in rest_api_default_filters(), which this PR doesn't touch. do_action_deprecated() / apply_filters_deprecated() are a separate concern and would need their own handler and suppression filter, feels like a separate ticket.
  • error_reporting() gate silencing WP_DEBUG_LOG: intentional; mirrors native PHP behavior as suggested.
  • Symlinked plugins outside WP_CONTENT_DIR: dev-only edge case not covered by the original loop either. Not a regression introduced here.
  • Three identical error_log() blocks: left inline for readability. Extracting a private helper felt like over-engineering for three call sites.
  • The doing_it_wrong_trigger_error filter question is still open; happy to add a separate filter (e.g. rest_doing_it_wrong_trigger_log) if you think it's worth it.g

"

#17 @wildworks
4 months ago

  • Milestone 7.1 → 7.2

With the 7.1 RC1 release imminent, ​PR 10537 still appears to have room for improvement, so we will postpone it to 7.2.

#18 @ravipatel
4 weeks ago

I've added PHPUnit coverage for the new _wp_rest_get_debug_backtrace_caller() helper from ​https://github.com/WordPress/wordpress-develop/pull/10537, as a PR into the author's branch: ​https://github.com/ilclaudio/wordpress-develop/pull/1

8 tests cover: named functions, instance and static methods, closures, calls from a file's global scope, the innermost WP_CONTENT_DIR frame, calls from outside WP_CONTENT_DIR, and a sibling directory such as wp-content-backup (the trailingslashit() case from review).

All pass on the PR's current head, and the tests fail if the trailingslashit(), closure, include/require or class-prefix handling is removed.

Note: the error_log() lines are not covered, because the test suite never defines WP_DEBUG_LOG.

Tests drafted with help from Claude Code; reviewed and verified by me.

​@ravipatel commented on ​PR #10537:


4 weeks ago
#19

I've added PHPUnit coverage for _wp_rest_get_debug_backtrace_caller() (8 tests: functions, methods, closures, global scope, nested plugins, outside wp-content, and the wp-content-backup sibling case) as a PR into your branch: ​https://github.com/ilclaudio/wordpress-develop/pull/1. Once merged, the tests will show up here.

​@ioclaudio commented on ​PR #10537:


4 weeks ago
#20

Thanks @rvpatel for the PHPUnit coverage of _wp_rest_get_debug_backtrace_caller() — it's merged into this branch and the checks are green.

@westonruter, here's where the PR stands after the latest rounds of review:

  • rest_handle_doing_it_wrong(), rest_handle_deprecated_function() and rest_handle_deprecated_argument() now write to the debug log during REST requests, with the PHP Notice: / PHP Deprecated: prefix, the $version information, and the plugin/theme caller (function, file and line).
  • Logging only happens when WP_DEBUG_LOG is enabled and the matching error_reporting() bit is set.
  • The shared backtrace logic lives in _wp_rest_get_debug_backtrace_caller() (renamed with the wp prefix as suggested by @t-hamano), hardened for missing function/line keys and for the wp-content-backup sibling-path case.
  • The 8 new tests cover functions, methods, closures, global scope, nested plugins, paths outside wp-content and the sibling-directory case.

The branch is up to date with trunk. Could you take another look when you have a moment? Your earlier "changes requested" review is still showing as blocking, so I'd appreciate a re-review (or dismissal if everything is addressed).

#21 @westonruter
3 days ago

  • Milestone 7.2 → 7.3

Sorry for the delay. I'm going to punt this because I'm not going to have time prior to 7.2 RC1.

​@wildworks commented on ​PR #10537:


22 hours ago
#22

@ilclaudio There's no need to repeatedly update the branch. While the milestone for this ticket has been moved to 7.3, this PR is still linked to it. Once the contributors have the bandwidth, this PR will be reviewed again. Thank you!

Note: See TracTickets for help on using tickets.