Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​URLHelper

Overview #

Methods #

NameReturn TypeSummary/Returns
file_system_to_url()stringTranslates a filesystem path to a URL

Returns: The URL derived from the filesystem path
get_content_subdir()stringGet the path to the content directory relative to the site.

Returns: (ex: /wp-content or /content)
get_current_url()stringGet the current URL of the page
get_full_path()string
get_host()stringSome setups like HTTP_HOST, some like SERVER_NAME, it's complicated

Returns: the HTTP_HOST or SERVER_NAME
get_params()array or string or falseReturns the url path parameters, or a single parameter if given an index.
get_path_base()string
get_rel_path()string
get_rel_url()string
get_scheme()stringGet url scheme
is_external()boolChecks whether a URL or domain is external.
is_external_content()boolThis function is slightly different from the one below in the case of: an image hosted on the same domain BUT on a different site than the WordPress install will be reported as external content.

Returns: if $url points to an external location returns true
is_local()boolChecks whether a URL or domain is local.
is_url()bool
prepend_to_url()stringAdd something to the start of the path in an URL

Returns: the result (ex 'https://nytimes.com/2017/news/article.html')
preslashit()stringAdd slash (if not already present) to a path
remove_double_slashes()stringLook for accidental slashes in a URL and remove them

Returns: the result (ex: https://nytimes.com/news/article.html)
remove_trailing_slash()stringPass links through untrailingslashit unless they are a single /
remove_url_component()stringRemoves the subcomponent of a URL regardless of protocol
starts_with()boolCheck to see if the URL begins with the string in question Because it's a URL we don't care about protocol (HTTP vs HTTPS) Or case (so it's cAsE iNsEnSiTiVe)
swap_protocol()stringSwaps whatever protocol of a URL is sent. http becomes https and vice versa

Returns: ex: https://example.org/wp-content/uploads/dog.jpg
unpreslashit()stringRemove slashes (if found) from a path
url_to_file_system()stringTranslates a URL to a filesystem path

Returns: The filesystem path derived from the URL
user_trailingslashit()stringPass links through user_trailingslashit handling query strings properly

Class Methods #

file_system_to_url() #

Translates a filesystem path to a URL

Takes a filesystem path and figures out its URL location.

file_system_to_url( string $fs )

Returns: string The URL derived from the filesystem path

NameTypeDescription
$fsstringThe filesystem path to translate to a URL

get_content_subdir() #

Get the path to the content directory relative to the site.

This replaces the WP_CONTENT_SUBDIR constant

Returns: string (ex: /wp-content or /content)


get_current_url() #

Get the current URL of the page

Returns: string


get_full_path() #

get_full_path( string $src )

Returns: string

NameTypeDescription
$srcstring

get_host() #

Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated

link https://stackoverflow.com/questions/2297403/http-host-vs-server-name

Returns: string the HTTP_HOST or SERVER_NAME


get_params() #

Returns the url path parameters, or a single parameter if given an index.

Returns false if given a non-existent index.

get_params( bool|int $i = false )

Returns: array|string|false

NameTypeDescription
$ibool or intthe position of the parameter to grab.

PHP

// Given a $_SERVER["REQUEST_URI"] of:
// https://example.org/blog/post/news/2014/whatever

$params = URLHelper::get_params();
// => ["blog", "post", "news", "2014", "whatever"]

$third = URLHelper::get_params(2);
// => "news"

// get_params() supports negative indices:
$last = URLHelper::get_params(-1);
// => "whatever"

$nada = URLHelper::get_params(99);
// => false

get_path_base() #

Returns: string


get_rel_path() #

get_rel_path( string $src )

Returns: string

NameTypeDescription
$srcstring

get_rel_url() #

get_rel_url( string $url, bool $force = false )

Returns: string

NameTypeDescription
$urlstring
$forcebool

get_scheme() #

Get url scheme

Returns: string


is_external() #

Checks whether a URL or domain is external.

True if the $url host name does not match the server’s host name. Otherwise, false.

is_external( string $url )

Returns: bool

NameTypeDescription
$urlstringURL to evaluate.

is_external_content() #

This function is slightly different from the one below in the case of: an image hosted on the same domain BUT on a different site than the WordPress install will be reported as external content.

is_external_content( string $url )

Returns: bool if $url points to an external location returns true

NameTypeDescription
$urlstringa URL to evaluate against

is_local() #

Checks whether a URL or domain is local.

True if $url has a host name matching the server’s host name. False if a relative URL or if it’s a subdomain.

is_local( string $url )

Returns: bool

NameTypeDescription
$urlstringURL to check.

is_url() #

is_url( string $url )

Returns: bool

NameTypeDescription
$urlstring

prepend_to_url() #

Add something to the start of the path in an URL

prepend_to_url( string $url, string $path )

Returns: string the result (ex 'https://nytimes.com/2017/news/article.html')

NameTypeDescription
$urlstringa URL that you want to manipulate (ex: 'https://nytimes.com/news/article.html').
$pathstringthe path you want to insert ('/2017').

preslashit() #

Add slash (if not already present) to a path

preslashit( string $path )

Returns: string

NameTypeDescription
$pathstringto process.

remove_double_slashes() #

Look for accidental slashes in a URL and remove them

remove_double_slashes( string $url )

Returns: string the result (ex: https://nytimes.com/news/article.html)

NameTypeDescription
$urlstringto process (ex: https://nytimes.com//news/article.html)

remove_trailing_slash() #

Pass links through untrailingslashit unless they are a single /

remove_trailing_slash( string $link )

Returns: string

NameTypeDescription
$linkstringthe URL to process.

remove_url_component() #

Removes the subcomponent of a URL regardless of protocol

since 1.3.3

remove_url_component( string $haystack, string $needle )

Returns: string

NameTypeDescription
$haystackstringex: https://example.org/wp-content/uploads/dog.jpg
$needlestringex: https://example.org/wp-content

starts_with() #

Check to see if the URL begins with the string in question Because it's a URL we don't care about protocol (HTTP vs HTTPS) Or case (so it's cAsE iNsEnSiTiVe)

starts_with( mixed $haystack, mixed $starts_with )

Returns: bool


swap_protocol() #

Swaps whatever protocol of a URL is sent. http becomes https and vice versa

since 1.3.3

swap_protocol( string $url )

Returns: string ex: https://example.org/wp-content/uploads/dog.jpg

NameTypeDescription
$urlstringex: https://example.org/wp-content/uploads/dog.jpg.

unpreslashit() #

Remove slashes (if found) from a path

unpreslashit( string $path )

Returns: string

NameTypeDescription
$pathstringto process.

url_to_file_system() #

Translates a URL to a filesystem path

Takes a url and figures out its filesystem location.

NOTE: Not fool-proof, makes a lot of assumptions about the file path matching the URL path

url_to_file_system( string $url )

Returns: string The filesystem path derived from the URL

NameTypeDescription
$urlstringThe URL to translate to a filesystem path

user_trailingslashit() #

Pass links through user_trailingslashit handling query strings properly

user_trailingslashit( string $link )

Returns: string

NameTypeDescription
$linkstringthe URL to process.