The get_home_path() WordPress PHP function retrieves the absolute filesystem path to the root of the WordPress installation.
Usage
// Retrieve the home path of the WordPress installation $home_path = get_home_path();
Parameters
- None
More information
See WordPress Developer Resources: get_home_path
Please note:
- This function is available only in the WordPress admin area (backend). To avoid errors, use
is_admin()
to check if you’re in the admin area before calling this function. - It may not return correct results when using WP_CLI.
Examples
Display the home path
Display the home path of your WordPress installation:
// Display the home path echo 'Home Path: ' . get_home_path();
Create a backup directory
Create a backup directory in the root of your WordPress installation:
// Set the backup directory path $backup_dir = get_home_path() . 'backups/'; // Create the directory if it doesn't exist if (!file_exists($backup_dir)) { mkdir($backup_dir, 0755, true); }
Check if a specific file exists in the root directory
Check if the wp-config.php
file exists in the root of your WordPress installation:
// Set the file path $file_path = get_home_path() . 'wp-config.php'; // Check if the file exists if (file_exists($file_path)) { echo 'The wp-config.php file exists.'; } else { echo 'The wp-config.php file does not exist.'; }
Display the list of files in the root directory
Display the list of files in the root of your WordPress installation:
// Retrieve the home path $home_path = get_home_path(); // Scan and display the list of files $files = scandir($home_path); echo 'Files in the root directory: ' . implode(', ', $files);
Create a robots.txt file if it doesn’t exist
Create a robots.txt
file in the root of your WordPress installation if it doesn’t exist:
// Set the robots.txt file path $robots_path = get_home_path() . 'robots.txt'; // Create the file if it doesn't exist if (!file_exists($robots_path)) { file_put_contents($robots_path, "User-agent: *\nDisallow:"); }