Using WordPress ‘path_join()’ PHP function

The path_join() WordPress PHP function joins two filesystem paths together.

Usage

$new_path = path_join( $base, $path );

Example:

$base = '/home/user/public_html';
$path = 'wp-content/uploads';
$new_path = path_join( $base, $path );
// Output: '/home/user/public_html/wp-content/uploads'

Parameters

  • $base (string) – Required. Base path.
  • $path (string) – Required. Path relative to $base.

More information

See WordPress Developer Resources: path_join

Examples

Combining paths for theme assets

Combine the path of the active theme with the path to a specific asset:

$theme_path = get_template_directory();
$asset_path = 'assets/css/style.css';
$full_path = path_join( $theme_path, $asset_path );

Concatenating paths for a custom plugin

Combine the path of a custom plugin directory with the path to a specific file within the plugin:

$plugin_path = plugin_dir_path( __FILE__ );
$file_path = 'includes/functions.php';
$full_path = path_join( $plugin_path, $file_path );

Building a path to a specific upload directory

Combine the path of the WordPress uploads directory with the path to a custom subdirectory:

$uploads = wp_upload_dir();
$custom_upload_path = 'my_custom_folder';
$full_path = path_join( $uploads['basedir'], $custom_upload_path );

Combining child theme path with a specific asset

Combine the path of the active child theme with the path to a specific asset:

$child_theme_path = get_stylesheet_directory();
$asset_path = 'assets/js/script.js';
$full_path = path_join( $child_theme_path, $asset_path );

Creating a path to a specific directory within the content folder

Combine the path of the WordPress content directory with the path to a specific subdirectory:

$content_directory = WP_CONTENT_DIR;
$custom_folder = 'my_custom_folder';
$full_path = path_join( $content_directory, $custom_folder );