WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#52199 closed defect (bug) (invalid)

Query string is expecting a array but is receiving a string

Reported by: zeshanb Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.6
Component: Permalinks Keywords:
Focuses: Cc:

Description

Hi there,

When trying to use the request filter in a template functions file Apache with PHP 8 is throwing this error:

 PHP Fatal error:  Uncaught TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in /home/freshinstall/public_html/wp-includes/class-wp.php:535
Stack trace:
#0 /home/freshinstall/public_html/wp-includes/class-wp.php(535): array_keys(NULL)
#1 /home/freshinstall/public_html/wp-includes/class-wp.php(623): WP->build_query_string()
#2 /home/freshinstall/public_html/wp-includes/class-wp.php(747): WP->query_posts()
#3 /home/freshinstall/public_html/wp-includes/functions.php(1291): WP->main('')
#4 /home/freshinstall/public_html/wp-blog-header.php(16): wp()
#5 /home/freshinstall/public_html/index.php(17): require('/home/freshinst...')
#6 {main}
  thrown in /home/freshinstall/public_html/wp-includes/class-wp.php on line 535

in wp-class.php following lines:

<?php
public function build_query_string() {
                $this->query_string = '';
                foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) {
                        if ( '' != $this->query_vars[ $wpvar ] ) {
                                $this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&';
                                if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars.
                                        continue;
                                }
                                $this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] );
                        }
                }

Following in funtions.php in theme:

<?php
function remove_page_from_query_string($query_string)
{
    
error_log("What the bleep is this query string? ". var_export($query_string) ."", 0);     
 
//return $query_string;
}

add_filter('request', 'remove_page_from_query_string');

When trying to just display it as a string using a error_log function:

<?php
error_log("What is this query string? ". $query_string ."", 0);  

In error log:

What is this query string? Array

Kind Regards,

Zeshan

Change History (3)

#1 @johnbillion
4 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thanks for the report @zeshanb but this appears to be a bug in your filter. You need to uncomment the //return $query_string; line, otherwise your filter returns null. Let us know if that's not the case.

#2 @SergeyBiryukov
4 months ago

Hi there, welcome to WordPress Trac! Thanks for the report.

Following in funtions.php in theme:

<?php
function remove_page_from_query_string($query_string)
{
    
error_log("What the bleep is this query string? ". var_export($query_string) ."", 0);     
 
//return $query_string;
}

add_filter('request', 'remove_page_from_query_string');

Just to clarify a bit more, since you're using add_filter(), you have to uncomment return $query_string; here, as a filter must always return a value. Otherwise, as it stands, that code is equivalent to:

add_filter( 'request', '__return_null' );

This would cause a warning in PHP 7.4 and earlier versions:

Warning: array_keys() expects parameter 1 to be array, null given in wp-includes/class-wp.php on line 535

and a fatal error in PHP 8.0 or later.

I think the warning or error is appropriate here, as WP::$query_vars is documented as an array, and expected to be an array in multiple other places. Returning null instead is a developer error and should be treated accordingly.

#3 @zeshanb
4 months ago

Hi,

Sorry the commented return may be causing a confusion.

I'm having the Array or string ($query_string) returned using the error log function to php error log file. The problem is it's neither being returned as a string or an array. When I load the index page, if it's an array it show display the initial key value, correct?

Regards,

Zeshan

Note: See TracTickets for help on using tickets.