Why You Can't Include PHP Files via Permalinks in WordPress
The core misunderstanding in your scenario lies in how WordPress handles routing versus how standard PHP scripts handle file inclusion. When you access a URL like http://localhost/?p=123 or http://localhost/?page_id=45, WordPress is not serving a physical file named after that path. Instead, the web server (usually configured with an .htaccess file) intercepts the request and forwards it to WordPress's core entry point, typically index.php.
WordPress then executes its internal logic: it identifies which page is being requested based on the query parameters (p=123 or page_id=45), loads the relevant template, and renders the HTML. Consequently, attempting to include a URL like include 'http://localhost/?page_id=45' fails because you are trying to execute the output of a rendered web page as PHP code, not the script itself. Furthermore, even if that worked conceptually, including an entire page's generated HTML into another file would likely result in syntax errors or broken layout logic, as the included content is already wrapped in tags and structure meant for display, not execution.
To share backend functions between files in a WordPress environment, you must bypass the routing layer entirely and interact with the server's filesystem directly. The standard approach involves creating a standalone PHP file on your diskâlet's call it my_utilities.phpâand including that file using the relative path or an absolute path within your theme or plugin files.
However, there is a critical architectural distinction to make here regarding where these functions live and how they are accessed. If you place my_utilities.php inside your active theme's directory (e.g., wp-content/themes/my-theme/my_utilities.php), you can include it directly:
// Inside your theme files, e.g., header.php or footer.php
require_once get_stylesheet_directory() . '/my_utilities.php';
Using require_once is generally safer than include because it ensures the file exists; if the utility functions are missing, the script will stop immediately rather than continuing with potentially broken logic. The get_stylesheet_directory() helper function resolves to your specific theme folder regardless of whether you are using a child theme or the parent theme, preventing paths from breaking during development.
If you prefer to keep all logic strictly separate from themes, you might place my_utilities.php in wp-content/ and include it via an absolute path relative to the WordPress root. This is common in plugins, but requires careful handling of server-relative paths if you intend to use it across different environments.
A significant pitfall arises when mixing frontend display logic with backend utility code. If your my_utilities.php file contains database queries or initialization logic that assumes a specific global state (like $wpdb being initialized), ensure that the WP environment is ready before the include statement runs. WordPress initializes its core classes during the bootstrap phase, so including utilities early in functions.php is usually safe, but including them inside template files (like header.php) happens after the page has started rendering. If your utility functions modify global variables or output directly, be cautious about where you place the include statement to avoid breaking the render flow.
The insert-php plugin mentioned works because it intercepts the final rendered HTML and attempts to parse it as code for preview purposes, which is distinct from actual file inclusion. It does not change how WordPress executes PHP files on the server.
For more complex architectures where you need to modularize your entire application logic away from the theme system, consider the trade-offs of building a custom MVC structure or utilizing frameworks like Laravel within a subdirectory. While possible, this requires significant configuration changes to Apache/Nginx and often involves disabling WordPress's default routing for specific paths. For most standard implementations, keeping utility files in wp-content/themes or wp-content/plugins and using relative path includes remains the robust, maintainable solution without reinventing the wheel.