Using WordPress ‘path_is_absolute()’ PHP function

The path_is_absolute() WordPress PHP function tests if a given filesystem path is absolute.

Usage

$path = '/foo/bar';
$is_absolute = path_is_absolute($path);

In this example, $is_absolute will be true if $path is an absolute path.

Parameters

  • $path (string) – Required. The file path to be tested.

More information

See WordPress Developer Resources: path_is_absolute()

Important Note: As of WordPress version 5.7, path_is_absolute() may return false on a Windows platform when used on a normalized (absolute) path (trac #48289).

Examples

Check if a path is absolute

Check if the given path is an absolute path.

$path = '/var/www/html/wp-content/uploads';
$is_absolute = path_is_absolute($path);
// $is_absolute will be true if the path is absolute

Check if a URL is absolute

Verify if the provided URL is an absolute URL.

$url = 'http://example.com/wp-content/uploads';
$is_absolute = path_is_absolute($url);
// $is_absolute will be false since the URL is not an absolute path

Check if a relative path is absolute

Determine if a relative path is an absolute path.

$relative_path = '../wp-content/uploads';
$is_absolute = path_is_absolute($relative_path);
// $is_absolute will be false since the path is relative

Check if a Windows path is absolute

Check if the given Windows file path is an absolute path.

$windows_path = 'C:\\Windows\\System32';
$is_absolute = path_is_absolute($windows_path);
// $is_absolute will be true if the path is absolute

Check if a file’s path is absolute

Check if the path of the current file is an absolute path.

$current_file_path = __FILE__;
$is_absolute = path_is_absolute($current_file_path);
// $is_absolute will be true if the path is absolute