[wp-trac] [WordPress Trac] #48272: "Undefined offset" warning in add_query_arg()
WordPress Trac
noreply at wordpress.org
Sat Oct 12 16:22:32 UTC 2019
#48272: "Undefined offset" warning in add_query_arg()
-------------------------------+------------------------------
Reporter: sumitsingh | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 5.2.3
Severity: normal | Resolution:
Keywords: reporter-feedback | Focuses: administration
-------------------------------+------------------------------
Comment (by jrf):
I've just looked into this and the suggested solution is incorrect.
As per the documentation,
[https://developer.wordpress.org/reference/functions/add_query_arg/
`add_query_arg()`] can be called in two ways:
{{{#!php
<?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
<?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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/48272#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list