path_join() WP 2.5.0
Join two filesystem paths together. If the second parameter is an absolute path (starts with a slash), then only it will be returned.
It works this way: give me the $path relative to $base, but if the $path is absolute, then just return it.
Automatically adds a slash /
between the paths.
Works based on: path_is_absolute()
1 time = 0.000061s = very fast | 50000 times = 0.05s = speed of light | PHP 7.1.1, WP 4.7.2
No Hooks.
Return
String. The path with the base or absolute path.
Usage
path_join( $base, $path );
- $base(string) (required)
- Base path.
- $path(string) (required)
- Path relative to $base.
Examples
#1 Demonstration of work
echo path_join('/var/site.ru', '/wp_content/uploads'); //> /wp_content/uploads echo path_join('/var/site.ru', 'wp_content/uploads'); //> /var/site.ru/wp_content/uploads echo path_join('/var/site.ru', ''); //> /var/site.ru/ echo path_join('/var/site.ru', '\path'); //> \path echo path_join('/var/site.ru', 'c:\\path'); //> c:\\path
#2 Include a file
Suppose we don't know whether the path of the $file variable is an absolute path to the file or it just the file name (relative path). In this case it's convenient to use this function:
$path = path_join( '/var/site/wp-content/', $file ); require_once( $path );
Now, no matter what the $file variable is: file.php
or /var/site/wp-content/file.php
— in both cases $path variable will be /var/site/wp-content/file.php
.
Changelog
Since 2.5.0 | Introduced. |
Code of path_join() path join WP 5.6
function path_join( $base, $path ) {
if ( path_is_absolute( $path ) ) {
return $path;
}
return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
}Related Functions
From tag: path (directory folder)
More from category: Helper Functions
- __return_empty_array()
- __return_empty_string()
- __return_false()
- __return_null()
- __return_true()
- __return_zero()
- build_query()
- get_page_hierarchy()
- human_readable_duration()
- is_email()
- is_php_version_compatible()
- is_serialized()
- is_serialized_string()
- is_wp_version_compatible()
- map_deep()
- maybe_serialize()
- maybe_unserialize()
- seems_utf8()
- stripslashes_deep()
- timer_stop()
- urlencode_deep()
- wp_array_slice_assoc()
- wp_debug_backtrace_summary()
- wp_doing_ajax()
- wp_extract_urls()
- wp_filter_object_list()
- wp_generate_uuid4()
- wp_html_split()
- wp_is_json_request()
- wp_is_numeric_array()
- wp_is_uuid()
- wp_json_encode()
- wp_kses_array_lc()
- wp_kses_hair()
- wp_kses_uri_attributes()
- wp_list_filter()
- wp_list_pluck()
- wp_list_sort()