Make WordPress Core

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's profile 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)

41745.patch (371 bytes) - added by kbjohnson90 7 years ago.

Download all attachments as: .zip

Change History (11)

#1 follow-up: @johnbillion
7 years ago

  • Keywords reporter-feedback added

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?

#2 @kbjohnson90
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.

@kbjohnson90
7 years ago

#3 @kbjohnson90
7 years ago

@johnbillion I have uploaded/attached a patch which has resolved the issue locally.

#4 @kbjohnson90
7 years ago

  • Keywords has-patch added

#5 @subrataemfluence
7 years ago

Hi, welcome to Trac and thanks for the ticket!
Can you please provide an example to reproduce the issue?

Last edited 7 years ago by subrataemfluence (previous) (diff)

#6 @kbjohnson90
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 @kbjohnson90
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

Last edited 7 years ago by kbjohnson90 (previous) (diff)

#9 @kbjohnson90
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.

Last edited 7 years ago by kbjohnson90 (previous) (diff)

#10 in reply to: ↑ description @cpdaaronjones
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.

Note: See TracTickets for help on using tickets.