WordPress.org

Make WordPress Core

#51552 closed defect (bug) (duplicate)

add_query_arg() does not allow multiple query variable pairs with same key name

Reported by: RavanH Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.5.1
Component: General Keywords:
Focuses: Cc:

Description

Best shown by example for construction of a URL for a font pair from Google Fonts following their new (css2) URL format:

<?php
// desired URL in this example, notice family= appearing twice:
// https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;1,400;1,700&family=Spartan:wght@200;400;500;700;800&display=swap

$fonts_url = 'https://fonts.googleapis.com/css2';
$fonts     = array(
        'Roboto:ital,wght@0,400;0,700;1,400;1,700',
        'Spartan:wght@200;400;500;700;800'
);

foreach ( $fonts as $font ) {
        $fonts_url = add_query_arg( 'family' , $font, $fonts_url );
}
$fonts_url = add_query_arg( 'display', 'swap', $fonts_url );

// $fonts_url will be with first key/value pair overwritten by the second key/value pair:
// https://fonts.googleapis.com/css2?family=Spartan%3Awght%40200%3B400%3B500%3B700%3B800&display=swap

Obviously, the way add_query_arg() is constructed using array key/value pairs, a pair with the same key name is not possible. However, in an URL query string, this is perfectly fine, so I'm marking this as a bug, not an enhancement. But that might be debated ;)

Change History (1)

#1 @ocean90
10 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Severity changed from minor to normal
  • Status changed from new to closed

Thanks for the report. This is a known issue and was already reported in #50106 and #49742.

Note: See TracTickets for help on using tickets.