WordPress at a glance

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: wp-includes/functions.php WP 5.2.4

<?php
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

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register