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.URL to which arguments should be appended. If omitted, only the resulting querystring is returned.
Query arguments to apply to URL.
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).
Data to encode.
Query string.
getQueryArg( url, arg )
Returns a single query argument of the URL.URL to parse.
Query arg name.
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.URL to parse.
Query args object.
getQueryString( url )
Returns the query string part of the URL.The full URL.
The query string part of the URL.
hasQueryArg( url, arg )
Determines whether the URL contains a given query arg.URL to check.
Query arg name.
Whether or not the URL contains the query arg.
removeQueryArgs( url, …args )
Removes arguments from the query string of the URL.URL to modify.
Query arg names to remove.
Updated URL.
URL Parsing
getProtocol( url )
Returns the protocol part of the URL.The full URL.
The protocol part of the URL.
getAuthority( url )
Returns the authority part of the URL.The full URL.
The authority part of the URL.
getPath( url )
Returns the path part of the URL.The full URL.
The path part of the URL.
getPathAndQueryString( url )
Returns the path part and query string part of the URL.The full URL.
The path part and query string part of the URL.
getFragment( url )
Returns the fragment part of the URL.The full URL.
The fragment part of the URL.
getFilename( url )
Returns the filename part of the URL.The full URL.
The filename part of the URL.
URL Validation
isURL( url )
Determines whether the given string looks like a URL.The string to scrutinise.
Whether or not it looks like a URL.
isEmail( email )
Determines whether the given string looks like an email.The string to scrutinise.
Whether or not it looks like an email.
isPhoneNumber( phoneNumber )
Determines whether the given string looks like a phone number.The string to scrutinize.
Whether or not it looks like a phone number.
isValidProtocol( protocol )
Tests if a URL protocol is valid.The URL protocol.
True if the argument is a valid protocol (e.g. http:, tel:).
isValidAuthority( authority )
Checks for invalid characters within the provided authority.A string containing the URL authority.
True if the argument contains a valid authority.
isValidPath( path )
Checks for invalid characters within the provided path.The URL path.
True if the argument contains a valid path.
isValidQueryString( queryString )
Checks for invalid characters within the provided query string.The query string.
True if the argument contains a valid query string.
isValidFragment( fragment )
Checks for invalid characters within the provided fragment.The URL fragment.
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.The URL to test.
The updated URL.
prependHTTPS( url )
Prepends “https://” to a URL, if it looks like something that is meant to be a TLD.The URL to test.
The updated URL.
This function will not replace “http://” with “https://“.
filterURLForDisplay( url, maxLength )
Returns a URL for display purposes.Original URL.
URL length limit.
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.
Title or slug to be processed.
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.Original path.
Normalized path.
Safe Decoding
safeDecodeURI( uri )
Safely decodes a URI withdecodeURI. Returns the URI unmodified if decodeURI throws an error.
URI to decode.
Decoded URI if possible.
safeDecodeURIComponent( uriComponent )
Safely decodes a URI component withdecodeURIComponent. Returns the URI component unmodified if decodeURIComponent throws an error.
URI component to decode.
Decoded URI component if possible.