Make WordPress Core

Opened 4 years ago

Last modified 3 years ago

#53262 new defect (bug)

wp_robots() (via wp_die) triggers a "doing_it_wrong_trigger_error", but should not.

Reported by: juliobox's profile juliobox Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 5.7
Component: General Keywords: has-patch needs-testing 2nd-opinion
Focuses: Cc:

Description

Hello there.

In my plugin, I need to die early, like, right after the plugins are loaded, imagine this for a shortcut:
add_action( 'plugins_loaded', 'wp_die' );
This is the output:
Notice: 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.) in /wp-includes/functions.php on line 5313

#	Time	Memory	Function				Location
1	0.0002	369912	{main}( )				.../admin.php:0
2	0.0003	370552	require_once( '/wp-load.php' )		.../admin.php:34
3	0.0003	370960	require_once( '/wp-config.php' )	.../wp-load.php:37
4	0.0003	375568	require_once( '/wp-settings.php' )	.../wp-config.php:88
5	0.0410	1735152	do_action( )				.../wp-settings.php:423
6	0.0411	1735528	WP_Hook->do_action( )			.../plugin.php:484
7	0.0411	1735528	WP_Hook->apply_filters( )		.../class-wp-hook.php:316
8	0.0882	3856336	wp_die( )				.../class-wp-hook.php:292
9	0.0882	3874480	_default_wp_die_handler( )		.../common.php:275
10	0.0884	3876304	wp_robots( )				.../functions.php:3497
11	0.0884	3876304	apply_filters( )			.../robots-template.php:32
12	0.0884	3876712	WP_Hook->apply_filters( )		.../plugin.php:212
13	0.0884	3878216	wp_robots_noindex_embeds( )		.../class-wp-hook.php:292
14	0.0884	3878216	is_embed( )				.../robots-template.php:93
15	0.0884	3878216	_doing_it_wrong( )			.../query.php:881
16	0.0885	3879304	trigger_error ( )			.../functions.php:5313

another notice will be trigger, same thing but line 13 will be wp_robots_noindex_search.

Since WP 5.7 the wp_robots() function is called in a wp_die(), but wp_die() can be called before the query is done, this is not too soon to die (for once, got it?).

A possible patch is to delay these default filters a bit later (wp-includes/default-filters.php) instead of adding them right away:

add_action( 'wp', 'wp_late_robots_check' );
function wp_late_robots_check() {
	add_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
	add_filter( 'wp_robots', 'wp_robots_noindex_search' );
}

Thanks for your attention.

Attachments (1)

fix-wp_die-notices.patch (467 bytes) - added by optimocha 3 years ago.
Fix wp_die() notices regarding early detections of is_embed and is_search

Download all attachments as: .zip

Change History (6)

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


4 years ago

#2 @lkraav
3 years ago

I can confirm, this is a real bug. In my case, a 3rd party plugin crashes real early, on plugins_loaded action.

Resulting stack trace

ErrorException: User Notice: is_search was called <strong>incorrectly</strong>. Conditional query tags do not work before the query is run. Before then, they always return false. 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.1.0.)
#11 /wp-includes/functions.php(5663): _doing_it_wrong
#10 /wp-includes/query.php(699): is_search
#9 /wp-includes/robots-template.php(119): wp_robots_noindex_search
#8 /wp-includes/class-wp-hook.php(303): WP_Hook::apply_filters
#7 /wp-includes/plugin.php(189): apply_filters
#6 /wp-includes/robots-template.php(32): wp_robots
#5 /wp-includes/functions.php(3675): _default_wp_die_handler
#4 /wp-includes/functions.php(3599): wp_die
#3 /wp-includes/class-wp-fatal-error-handler.php(233): WP_Fatal_Error_Handler::display_default_error_template
#2 /wp-includes/class-wp-fatal-error-handler.php(152): WP_Fatal_Error_Handler::display_error_template
#1 /wp-includes/class-wp-fatal-error-handler.php(57): WP_Fatal_Error_Handler::handle
#0 [internal](0): null

#3 @lkraav
3 years ago

Maybe @flixos90 has ideas here, since he brought in wp_robots() in #51511?

@optimocha
3 years ago

Fix wp_die() notices regarding early detections of is_embed and is_search

#4 @optimocha
3 years ago

  • Keywords has-patch needs-testing 2nd-opinion added

I can confirm these notices about is_embed() and is_search are still being thrown in version 6.0, and I'm attaching a patch to get rid of them.

I must admit the patch doesn't seem to have the right way to fix this problem but since there's an add_filter() line right above the remove_filter() lines I'm adding, it felt somewhat proper. Still, I'd like a second opinion for the patch.

#5 @Starbuck
3 years ago

Verified in 6.0.1 ... open ticket so no surprise, just confirming. :)

#1 /wp-includes/functions.php(5831): trigger_error()
#2 /wp-includes/query.php(881): _doing_it_wrong()
#3 /wp-includes/robots-template.php(93): is_embed()
#4 /wp-includes/class-wp-hook.php(307): wp_robots_noindex_embeds()
#5 /wp-includes/plugin.php(191): WP_Hook->apply_filters()
#6 /wp-includes/robots-template.php(32): apply_filters()
Note: See TracTickets for help on using tickets.