Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#50406 closed enhancement (fixed)

Make _wp_scripts_maybe_doing_it_wrong helpful by adding offending handle to debug output

Reported by: janthiel's profile janthiel Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.5 Priority: normal
Severity: normal Version: 5.5
Component: Script Loader Keywords: has-patch
Focuses: Cc:

Description

Triggering a

_wp_scripts_maybe_doing_it_wrong

for example here:

function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

renders the following Debug Notice:

PHP Notice:  wp_enqueue_style was called <strong>incorrectly</strong>. Scripts and styles should not be registered or enqueued until the <code>wp_enqueue_scripts</code>, <code>admin_enqueue_scripts</code>, or <code>login_enqueue_scripts</code> hooks. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.3.0.) in /var/www/clients/client12/web585/web/wp-includes/functions.php on line 5167

This does not tell anything helpful solving the issue. There is no backtrace or information which could help identifing the bad actor (mostly a Plugin or Theme).

Adding the handle of the style / script will result in a very useful debug information.

After applying the attached patch, the debug output will look like this:

PHP Notice:  wp_enqueue_style was called <strong>incorrectly</strong>. Scripts and styles should not be registered or enqueued until the <code>wp_enqueue_scripts</code>, <code>admin_enqueue_scripts</code>, or <code>login_enqueue_scripts</code> hooks. This was violated by the script or style with the handle bad-style-handle. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 3.3.0.) in /var/www/clients/client12/web585/web/wp-includes/functions.php on line 5167

This is the added part:

This was violated by the script or style with the handle bad-style-handle.

Attachments (1)

Add_information_about_violating_handle_to__doint_it_wrong_output_.patch (7.0 KB) - added by janthiel 4 years ago.

Download all attachments as: .zip

Change History (4)

#1 @SergeyBiryukov
4 years ago

  • Component changed from General to Script Loader

#2 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.5

#3 @SergeyBiryukov
4 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 48070:

Script Loader: Include the script or style handle in _wp_scripts_maybe_doing_it_wrong() message.

This makes the message more helpful and allows for easier debugging.

Props janthiel.
Fixes #50406.

Note: See TracTickets for help on using tickets.