WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 3 months 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)

41745.patch (371 bytes) - added by kbjohnson90 4 months ago.

Download all attachments as: .zip

Change History (10)

#1 follow-up: @johnbillion
4 months 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
4 months 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
4 months ago

#3 @kbjohnson90
4 months ago

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

#4 @kbjohnson90
4 months ago

  • Keywords has-patch added

#5 @subrataemfluence
3 months ago

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

Last edited 3 months ago by subrataemfluence (previous) (diff)

#6 @kbjohnson90
3 months 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
3 months 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 3 months ago by kbjohnson90 (previous) (diff)

#9 @kbjohnson90
3 months 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 3 months ago by kbjohnson90 (previous) (diff)
Note: See TracTickets for help on using tickets.