Make WordPress Core

Opened 6 months ago

Last modified 7 weeks ago

#48272 new defect (bug)

"Undefined offset" warning in add_query_arg()

Reported by: sumitsingh Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.2.3
Component: General Keywords: reporter-feedback
Focuses: administration Cc:

Description (last modified by SergeyBiryukov)


When I am login admin side then getting notice. because I have enabled error_log true in the config file.

===========Notice error=============

<br />
<b>Notice</b>:  Undefined offset: 1 in <b>/var/www/html/wpfdemo/wp-includes/functions.php</b> on line <b>964</b><br />


$qs[ $args[0] ] = $args[1]; 


$qs[ $args[0] ] = $args[0];


Please let us know if this helps.


Change History (7)

#1 @knutsp
6 months ago

  • Keywords reporter-feedback added

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

It seems to me some code in your installation is using the function add_query_arg the wrong way. Does this notice appear with no active plugins and a default theme?

I can recommend this plugin to help troubleshooting and find the offending plugin, using it's Troubleshooting mode https://wordpress.org/plugins/health-check/

#2 @knutsp
6 months ago

  • Focuses coding-standards removed

#3 @SergeyBiryukov
6 months ago

  • Description modified (diff)

#4 @SergeyBiryukov
6 months ago

  • Summary changed from Error on version 5.2.3 to "Undefined offset" warning in add_query_arg()

#5 @jrf
6 months ago

I've just looked into this and the suggested solution is incorrect.

As per the documentation, `add_query_arg()` can be called in two ways:

// Using a single key and value:
add_query_arg( 'key', 'value', 'http://example.com' );

// Using an associative array:
add_query_arg( array(
    'key1' => 'value1',
    'key2' => 'value2',
), 'http://example.com' );

The code which is causing the notice is the else case here:

        if ( is_array( $args[0] ) ) {
                foreach ( $args[0] as $k => $v ) {
                        $qs[ $k ] = $v;
        } else {
                $qs[ $args[0] ] = $args[1];

The only way the error could be created is if add_query_arg() gets called with only one argument, not two or three and that one argument is a string, in other words, the single key and value pattern from above, but without a value.

First off: I expect this to be a code error in the code calling the function, so I would like to see a backtrace so we can determine what plugin or theme is causing it and then it should be reported to the plugin/theme author.

Secondly, I'm not completely read up on the RFCs which apply to URL build-ups, so someone who is will need to chime in here, but basically it needs to be determined whether setting a key without a value as part of the query string should be allowed at all.

Think: https://example.com/?key1=value1&key2&key3=value3 <= key2 in this URL.

#6 @sumitsingh
4 months ago


Now working fine for me.


#7 @SergeyBiryukov
7 weeks ago

#49391 was marked as a duplicate.

Note: See TracTickets for help on using tickets.