Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#48272 new defect (bug)

"Undefined offset" warning in add_query_arg()

Reported by: sumitsingh's profile 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)

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 (9)

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

  • Focuses coding-standards removed

#3 @SergeyBiryukov
5 years ago

  • Description modified (diff)

#4 @SergeyBiryukov
5 years ago

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

#5 @jrf
5 years 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.

#6 @sumitsingh
5 years ago

Hi,

Now working fine for me.

Thanks

#7 @SergeyBiryukov
5 years ago

#49391 was marked as a duplicate.

#8 @clnumendo
5 years ago

Hello,

so, if we can't call the function with one string argument without trigger PHP notices, maybe you should modify the documentation that tell us that only one argument is required.

https://developer.wordpress.org/reference/functions/add_query_arg/

$key
(string|array) (Required) Either a query variable key, or an associative array of query variables.

$value
(string) (Optional) Either a query variable value, or a URL to act upon.

$url
(string) (Optional) A URL to act upon.

Indeed, i want to show you what my error.log look like ;)

[04-May-2020 18:48:02 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:02 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:03 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:03 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:05 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:05 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:06 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:06 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:07 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:07 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:08 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:08 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:10 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:10 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:10 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:10 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:11 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:11 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:12 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:12 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:50 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:50 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:53 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:48:53 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:49:19 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:49:19 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:49:20 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:49:20 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:50:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:50:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:50:52 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:50:52 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:51:20 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:51:20 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:51:23 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:51:23 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:52:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:52:51 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:52:52 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:52:52 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:53:21 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:53:21 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:53:22 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
[04-May-2020 18:53:22 Europe/Paris] PHP Notice:  Undefined offset: 1 in /var/www/html/wp-includes/functions.php on line 1131
...

Thanks

#9 @sumitsingh
3 years ago

Hey,

Very old ticket is pending and it hink now fixed. So can we check this ticket anyone?

Note: See TracTickets for help on using tickets.