URL
The@wordpress/url package provides a comprehensive collection of utilities for manipulating URLs in JavaScript. It includes functions for working with query strings, validating URL components, and parsing URL parts.
Installation
You can install the package using npm:This package assumes your code will run in an ES2015+ environment. If you’re targeting environments with limited support for modern JavaScript features, include the polyfill from
@wordpress/babel-preset-default.Query String Utilities
addQueryArgs( url, args )
Appends arguments as querystring to the provided URL. If the URL already includes query arguments, the arguments are merged with (and take precedent over) the existing set.string
required
URL to which arguments should be appended. If omitted, only the resulting querystring is returned.
object
required
Query arguments to apply to URL.
string
URL with arguments applied.
buildQueryString( data )
Generates URL-encoded query string using input query data. This function behaves equivalent to PHP’shttp_build_query, configured with encoding type PHP_QUERY_RFC3986 (spaces as %20).
object
required
Data to encode.
string
Query string.
getQueryArg( url, arg )
Returns a single query argument of the URL.string
required
URL to parse.
string
required
Query arg name.
string | undefined
Query arg value.
getQueryArgs( url )
Returns an object of query arguments of the given URL. If the URL is invalid or has no querystring, an empty object is returned.string
required
URL to parse.
object
Query args object.
getQueryString( url )
Returns the query string part of the URL.string
required
The full URL.
string | void
The query string part of the URL.
hasQueryArg( url, arg )
Determines whether the URL contains a given query arg.string
required
URL to check.
string
required
Query arg name.
boolean
Whether or not the URL contains the query arg.
removeQueryArgs( url, …args )
Removes arguments from the query string of the URL.string
required
URL to modify.
string[]
required
Query arg names to remove.
string
Updated URL.
URL Parsing
getProtocol( url )
Returns the protocol part of the URL.string
required
The full URL.
string | void
The protocol part of the URL.
getAuthority( url )
Returns the authority part of the URL.string
required
The full URL.
string | void
The authority part of the URL.
getPath( url )
Returns the path part of the URL.string
required
The full URL.
string | void
The path part of the URL.
getPathAndQueryString( url )
Returns the path part and query string part of the URL.string
required
The full URL.
string
The path part and query string part of the URL.
getFragment( url )
Returns the fragment part of the URL.string
required
The full URL.
string | void
The fragment part of the URL.
getFilename( url )
Returns the filename part of the URL.string
required
The full URL.
string | void
The filename part of the URL.
URL Validation
isURL( url )
Determines whether the given string looks like a URL.string
required
The string to scrutinise.
boolean
Whether or not it looks like a URL.
isEmail( email )
Determines whether the given string looks like an email.string
required
The string to scrutinise.
boolean
Whether or not it looks like an email.
isPhoneNumber( phoneNumber )
Determines whether the given string looks like a phone number.string
required
The string to scrutinize.
boolean
Whether or not it looks like a phone number.
isValidProtocol( protocol )
Tests if a URL protocol is valid.string
required
The URL protocol.
boolean
True if the argument is a valid protocol (e.g. http:, tel:).
isValidAuthority( authority )
Checks for invalid characters within the provided authority.string
required
A string containing the URL authority.
boolean
True if the argument contains a valid authority.
isValidPath( path )
Checks for invalid characters within the provided path.string
required
The URL path.
boolean
True if the argument contains a valid path.
isValidQueryString( queryString )
Checks for invalid characters within the provided query string.string
required
The query string.
boolean
True if the argument contains a valid query string.
isValidFragment( fragment )
Checks for invalid characters within the provided fragment.string
required
The URL fragment.
boolean
True if the argument contains a valid fragment.
URL Manipulation
prependHTTP( url )
Prepends “http://” to a URL, if it looks like something that is meant to be a TLD.string
required
The URL to test.
string
The updated URL.
prependHTTPS( url )
Prepends “https://” to a URL, if it looks like something that is meant to be a TLD.string
required
The URL to test.
string
The updated URL.
This function will not replace “http://” with “https://“.
filterURLForDisplay( url, maxLength )
Returns a URL for display purposes.string
required
Original URL.
number | null
URL length limit.
string
Displayed URL.
cleanForSlug( string )
Performs basic cleanup of a string for use as a post slug. This approximates whatsanitize_title_with_dashes() does in WordPress core.
string
required
Title or slug to be processed.
string
Processed string.
This function converts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters, removes combining diacritical marks, converts whitespace/periods/slashes to hyphens, removes non-word characters except hyphens, and converts to lowercase.
normalizePath( path )
Returns a normalized path where equal query parameter values will be treated as identical, regardless of order.string
required
Original path.
string
Normalized path.
Safe Decoding
safeDecodeURI( uri )
Safely decodes a URI withdecodeURI. Returns the URI unmodified if decodeURI throws an error.
string
required
URI to decode.
string
Decoded URI if possible.
safeDecodeURIComponent( uriComponent )
Safely decodes a URI component withdecodeURIComponent. Returns the URI component unmodified if decodeURIComponent throws an error.
string
required
URI component to decode.
string
Decoded URI component if possible.