WordPress.org

Make WordPress Core

Opened 8 days ago

Last modified 6 days 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:
PR Number:

Description (last modified by SergeyBiryukov)

Hi,

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 />

============Solution===============

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

To

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

=================================

Please let us know if this helps.

Thanks

Change History (5)

#1 @knutsp
7 days 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
7 days ago

  • Focuses coding-standards removed

#3 @SergeyBiryukov
6 days ago

  • Description modified (diff)

#4 @SergeyBiryukov
6 days ago

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

#5 @jrf
6 days 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:

<?php
// 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:

<?php
        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.

Note: See TracTickets for help on using tickets.