Timber\URLHelper
Overview #
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
file_system_to_url() | string | Translates a filesystem path to a URL Returns: The URL derived from the filesystem path |
get_content_subdir() | string | Get the path to the content directory relative to the site. Returns: (ex: /wp-content or /content) |
get_current_url() | string | Get the current URL of the page |
get_full_path() | string | |
get_host() | string | Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated Returns: the HTTP_HOST or SERVER_NAME |
get_params() | array or string or false | Returns 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() | string | Get url scheme |
is_external() | bool | Checks whether a URL or domain is external. |
is_external_content() | bool | 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. Returns: if $url points to an external location returns true |
is_local() | bool | Checks whether a URL or domain is local. |
is_url() | bool | |
prepend_to_url() | string | Add something to the start of the path in an URL Returns: the result (ex 'https://nytimes.com/2017/news/article.html') |
preslashit() | string | Add slash (if not already present) to a path |
remove_double_slashes() | string | Look for accidental slashes in a URL and remove them Returns: the result (ex: https://nytimes.com/news/article.html) |
remove_trailing_slash() | string | Pass links through untrailingslashit unless they are a single / |
remove_url_component() | string | Removes the subcomponent of a URL regardless of protocol |
starts_with() | bool | 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) |
swap_protocol() | string | Swaps whatever protocol of a URL is sent. http becomes https and vice versa Returns: ex: https://example.org/wp-content/uploads/dog.jpg |
unpreslashit() | string | Remove slashes (if found) from a path |
url_to_file_system() | string | Translates a URL to a filesystem path Returns: The filesystem path derived from the URL |
user_trailingslashit() | string | Pass links through user_trailingslashit handling query strings properly |
Class Methods #
get_current_url() #
Get the current URL of the page
Returns: string
get_scheme() #
Get url scheme
Returns: string
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
is_url() #
is_url( string $url )
Returns: bool
Name | Type | Description |
---|---|---|
$url | string |
get_path_base() #
Returns: string
get_rel_url() #
get_rel_url( string $url, bool $force = false )
Returns: string
Name | Type | Description |
---|---|---|
$url | string | |
$force | bool |
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
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
Name | Type | Description |
---|---|---|
$url | string | URL to check. |
get_full_path() #
get_full_path( string $src )
Returns: string
Name | Type | Description |
---|---|---|
$src | string |
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
Name | Type | Description |
---|---|---|
$url | string | The URL to translate to a filesystem path |
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
Name | Type | Description |
---|---|---|
$fs | string | The 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_rel_path() #
get_rel_path( string $src )
Returns: string
Name | Type | Description |
---|---|---|
$src | string |
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)
Name | Type | Description |
---|---|---|
$url | string | to process (ex: https://nytimes.com//news/article.html) |
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')
Name | Type | Description |
---|---|---|
$url | string | a URL that you want to manipulate (ex: 'https://nytimes.com/news/article.html'). |
$path | string | the path you want to insert ('/2017'). |
preslashit() #
Add slash (if not already present) to a path
preslashit( string $path )
Returns: string
Name | Type | Description |
---|---|---|
$path | string | to process. |
unpreslashit() #
Remove slashes (if found) from a path
unpreslashit( string $path )
Returns: string
Name | Type | Description |
---|---|---|
$path | string | to process. |
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
Name | Type | Description |
---|---|---|
$url | string | a URL to evaluate against |
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
Name | Type | Description |
---|---|---|
$url | string | URL to evaluate. |
remove_trailing_slash() #
Pass links through untrailingslashit unless they are a single /
remove_trailing_slash( string $link )
Returns: string
Name | Type | Description |
---|---|---|
$link | string | the 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
Name | Type | Description |
---|---|---|
$haystack | string | ex: https://example.org/wp-content/uploads/dog.jpg |
$needle | string | ex: https://example.org/wp-content |
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
Name | Type | Description |
---|---|---|
$url | string | ex: https://example.org/wp-content/uploads/dog.jpg. |
user_trailingslashit() #
Pass links through user_trailingslashit handling query strings properly
user_trailingslashit( string $link )
Returns: string
Name | Type | Description |
---|---|---|
$link | string | the URL to process. |
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
Name | Type | Description |
---|---|---|
$i | bool or int | the 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