How can I include Wordpress functions in a custom .php file?
In detail: I have a directory under my theme (Constructor) named reports. These contain .php files that generate reports from data from the site with DOMPDF for downloading. For these I would like to use functions that the Wordpress engine provides, for example get_the_author_meta( 'user_firstname', $user_id )
. If I use these i get (naturally) the following error:
Fatal error: Call to undefined function get_the_author_meta() in ROOT/public_html/wp-content/themes/constructor/reports/testreport.php on line 15
I was lead to believe that I need to include wp-blog-header.php . I use require_once("../../../../wp-blog-header.php");
. With this I get the following 404 error:
No webpage was found for the web address: ROOT/wp-content/themes/constructor/reports/testreport.php
(The require points to the correct path. If I fiddle with it, I get Warning: require_once(../../../wp-blog-header.php): failed to open stream... So the path must be correct.)
Is there something I overlook? Why can't I include this wp file? What is the correct method to include the wp functions?
Thanks for the help, Sziro
External files can easily access the wordpress functions. You just need to include the file
wp-load.php
in your external file. Thewp-load.php
file is located in root of your wordpress installation. Example: Suppose your file istest.php
located at root directory of wordpress installation.Source: How to access WordPress functions in external file
The more professional way without the dots thing if WordPress was the document root:
You're on the right track. Try this instead:
This is better way to include a file in wordpress
Well if someone has newer PHP versions installed (ver >= 5.5.x) then they can also try the below code in the root script in wordpress website directory itself:
Or
I guess this is a more direct and clean approach and doesn't involve manually adding slashes and changing diretories by
..
.Hope this helps someone.