WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 4 weeks ago

#53262 new defect (bug)

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

Reported by: juliobox Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 5.7
Component: General Keywords:
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.

Change History (1)

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


4 weeks ago

Note: See TracTickets for help on using tickets.