Opened 7 years ago
Last modified 3 years ago
#41745 new defect (bug)
Uncaught Error in get_query_var when $wp_query is null
Reported by: | kbjohnson90 | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.8.1 |
Component: | Query | Keywords: | has-patch |
Focuses: | Cc: |
Description
When get_query_var()
is called without the global $wp_query being set an Uncaught Exception is thrown.
Attachments (1)
Change History (11)
#2
@
7 years ago
- Keywords reporter-feedback removed
- Summary changed from Uncaught Exception in get_query_var when $wp_query is null to Uncaught Error in get_query_var when $wp_query is null
Rather, it is an Uncaught Error
, not an exception.
Fatal error: Uncaught Error: Call to a member function get() on null in /app/public/wp-includes/query.php on line 29
Error: Call to a member function get() on null in /app/public/wp-includes/query.php on line 29
The addition of a check and early return should resolve this issue.
<?php function get_query_var( $var, $default = '' ) { global $wp_query; if( ! isset( $wp_query ) || ! method_exists( $wp_query, 'get' ) ) return $default; return $wp_query->get( $var, $default ); }
wp-includes/query.php#26
Looking at submitting a patch, but still new to Trac.
#3
@
7 years ago
@johnbillion I have uploaded/attached a patch which has resolved the issue locally.
#5
@
7 years ago
Hi, welcome to Trac and thanks for the ticket!
Can you please provide an example to reproduce the issue?
#6
@
7 years ago
I was able to reproduce it was a rather convoluted combination of plugins:
Event Manager
Event Manager Pro
WP Popup Maker
Ninja Forms
The error is thrown in Event Manager Pro, but only when WP Popup Maker is active, but only when the Ninja Forms THREE integration is active.
Event Manager Pro is calling get_query_var()
inside of the hook parse_query
.
WP Popup Maker is calling get_posts()
inside of the hook plugins_loaded
.
I'll see if I can put together a reduced test case.
#7
@
7 years ago
Here is a reduced test case:
<?php function my_plugins_loaded(){ $posts = get_posts(); } add_action( 'plugins_loaded', 'my_plugins_loaded' ); function my_parse_query(){ $var = get_query_var( 'foo', false ); } add_action( 'parse_query', 'my_parse_query' );
https://gist.github.com/kjohnson/c93e6d5701778149bcf53a8c11afe470
#8
in reply to:
↑ 1
@
7 years ago
Replying to johnbillion:
WordPress core doesn't use exceptions, so this error is probably coming from a plugin or theme on your site.
It looks like there are a few exceptions in core. For example:
Is there an official core policy about exceptions? I didn't see one in https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/.
#9
@
7 years ago
@dlh @johnbillion to clarify, the issue here is an Uncaught Error
. The mention of an Exception was a typo on my part.
#10
in reply to:
↑ description
@
3 years ago
Replying to kbjohnson90:
When
get_query_var()
is called without the global $wp_query being set an Uncaught Exception is thrown.
Is there anyway we can get this merged into live? Our .gov site was affected by the perfect combination of plugins as well and this fix applies to us. We are currently running the patch and it has resolved our issues but it would help tremendously if this survived updates please.
Thanks for the report, @kbjohnson90, and welcome to WordPress Trac.
WordPress core doesn't use exceptions, so this error is probably coming from a plugin or theme on your site. Can you let us know the exact exception message please?