The get_file() WordPress PHP function retrieves the content of a file and returns it as a string.
Usage
To use the get_file() function, simply provide the file path as an argument:
$content = get_file('path/to/your/file.txt'); echo $content;
Parameters
- file_path (string): The path to the file you want to retrieve the content from.
More information
See WordPress Developer Resources: get_file()
Examples
Read and display the content of a text file
// Read the content of 'file.txt' and store it in the $content variable $content = get_file('file.txt'); // Display the content of the file echo $content;
Read and display a CSS file
// Read the content of 'style.css' and store it in the $css variable $css = get_file('style.css'); // Output the CSS content as an inline style echo '<style>' . $css . '</style>';
Read and display an HTML file as part of a template
// Read the content of 'header.html' and store it in the $header variable $header = get_file('header.html'); // Output the HTML content as part of the template echo $header;
Read a configuration file and parse it as JSON
// Read the content of 'config.json' and store it in the $config variable $config = get_file('config.json'); // Parse the JSON content and store it in the $config_array variable $config_array = json_decode($config, true); // Access the 'title' key from the configuration array $title = $config_array['title'];
Read and display the content of a log file
// Read the content of 'error.log' and store it in the $log variable $log = get_file('error.log'); // Display the content of the log file as preformatted text echo '<pre>' . $log . '</pre>';