How to include WordPress functions in custom .php file?

How to include WordPress functions in custom .php file?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why wp-blog-header.php Fails and How to Actually Load WordPress Functions

You are encountering a classic misunderstanding of how the WordPress bootstrap process works. The error you are seeing—Fatal error: Call to undefined function get_the_author_meta()—happens because your custom file is executing completely outside the scope of WordPress. When PHP loads wp-blog-header.php manually from within a theme directory, it triggers a redirect loop or attempts to output an HTML response that conflicts with your script's execution context.

The file wp-blog-header.php is responsible for initializing the session, loading the theme data, and ultimately sending headers to either display a page or redirect the browser. It expects to be called by the main entry point (index.php) or load.php at a very specific moment in the lifecycle of a request. When you call it directly from a script intended to generate PDFs or process data, you are bypassing the safety checks and initialization hooks that WordPress relies on.

The Correct Entry Point: wp-load.php

To access WordPress functions outside of the standard page load cycle—such as inside an AJAX handler, a CLI command, or a standalone report generator—you must include the bootstrap file located at /wp-load.php.

This file is distinct from wp-blog-header.php. While wp-load.php loads the core classes and defines the global $wpdb object and functions like get_the_author_meta, it does not attempt to send headers or redirect the browser immediately. It prepares the environment for WordPress without assuming a specific output format.

Update your include statement to point to this file instead:

// Path relative to the theme root where you are currently located
require_once( dirname( __FILE__ ) . '/../../../../wp-load.php' );

Or, if you prefer an absolute path from the project root (assuming wp-content is a subfolder):

define( 'WP_USE_THEMES', false ); // Disable theme loading to speed up execution and avoid conflicts
require_once( dirname( __DIR__ ) . '/wp-load.php' );

Critical Implementation Details

Simply including the file is not enough; you must understand why this change fixes the 404 error and how to handle the output.

When you previously used require_once("../../../../wp-blog-header.php"), WordPress interpreted this as a request to load a specific theme page. Because your script was trying to process data (likely returning JSON or generating PDF content) rather than rendering an HTML template, WordPress tried to redirect the user to the current URL (which became the full path to your custom PHP file). This mismatch caused the browser to interpret the response as a 404 error or a redirect loop.

By switching to wp-load.php, you bypass the theme loading and output logic until explicitly told otherwise. However, there is one crucial flag you should define before including the bootstrap file: WP_USE_THEMES.

Setting this constant to false prevents WordPress from trying to load your theme files (like header.php or footer.php) and render HTML. This is essential for scripts that run in the background or generate downloadable reports. Without this flag, WordPress might attempt to enqueue CSS/JS assets meant for a live site, bloating your report generation time and potentially causing conflicts with DOMPDF rendering.

Handling DOMPDF and Output

Since your goal is to use libraries like DOMPDF, you must ensure that WordPress does not try to send headers before the PDF generation begins. If WordPress sends a Content-Type header for an HTML page first, DOMPDF will fail to buffer the output correctly.

The sequence of operations in your custom file should look like this:

  1. Define WP_USE_THEMES as false.
  2. Include wp-load.php.
  3. Initialize WordPress functions (get_the_author_meta, etc.).
  4. Generate your data or HTML string.
  5. Pass that string to DOMPDF.
  6. Send the final output headers and stream the file.

Example implementation:

<?php
// Ensure we don't load themes or enqueue assets for this script
define( 'WP_USE_THEMES', false );

// Load WordPress core functions and global variables
require_once dirname( __FILE__ ) . '/../../../../wp-load.php';

// Now you can safely use WordPress functions
$user_id = 5;
$author_name = get_the_author_meta( 'first_name', $user_id );

// Proceed with your DOMPDF logic here
// ...

Conclusion on Architecture

While including wp-load.php solves the immediate technical error, it is worth noting that for large-scale report generation, loading the entire WordPress core can be resource-intensive. If you find yourself needing to run these reports frequently, consider building a dedicated CLI tool or using a separate API endpoint in your theme's functions.php that handles the logic without requiring manual inclusion every time. However, for ad-hoc scripts within the theme directory, wp-load.php remains the standard and most reliable method to access the WordPress API safely.

Our network