diff --git a/src/wp-includes/js/api-request.js b/src/wp-includes/js/api-request.js index f0a84ef48f..4b299beb69 100644 --- a/src/wp-includes/js/api-request.js +++ b/src/wp-includes/js/api-request.js @@ -22,7 +22,7 @@ apiRequest.buildAjaxOptions = function( options ) { var url = options.url; var path = options.path; - var namespaceTrimmed, endpointTrimmed; + var namespaceTrimmed, endpointTrimmed, apiRoot; var headers, addNonceHeader, headerName; if ( @@ -38,7 +38,16 @@ } } if ( typeof path === 'string' ) { - url = wpApiSettings.root + path.replace( /^\//, '' ); + apiRoot = wpApiSettings.root; + path = path.replace( /^\//, '' ); + + // API root may already include query parameter prefix if site is + // configured to use plain permalinks. + if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) { + path = path.replace( '?', '&' ); + } + + url = apiRoot + path; } // If ?_wpnonce=... is present, no need to add a nonce header. diff --git a/tests/qunit/wp-includes/js/api-request.js b/tests/qunit/wp-includes/js/api-request.js index 99a1213ea3..86d0d72cda 100644 --- a/tests/qunit/wp-includes/js/api-request.js +++ b/tests/qunit/wp-includes/js/api-request.js @@ -140,9 +140,9 @@ window.wpApiSettings.root = 'http://localhost/index.php?rest_route=/'; assert.deepEqual( wp.apiRequest.buildAjaxOptions( { namespace: '/wp/v2/', - endpoint: '/posts' + endpoint: '/posts?orderby=title' } ), { - url: 'http://localhost/index.php?rest_route=/wp/v2/posts', + url: 'http://localhost/index.php?rest_route=/wp/v2/posts&orderby=title', headers: nonceHeader } ); }