84

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

Maxime
  • 8,645
  • 5
  • 50
  • 53
Sziro
  • 1,273
  • 1
  • 13
  • 21
  • For Gutenberg users: https://wpza.net/including-wordpress-functions-in-a-custom-php-file/ – WPZA Jan 25 '19 at 21:44

6 Answers6

153

You're on the right track. Try this instead:

require_once("../../../../wp-load.php");
seanbreeden
  • 6,104
  • 5
  • 36
  • 45
32

To use wp functions in custom .php files, you must include wp-load.php in your file. You can do so by adding the following line:

require_once(PATH_TO.'/wp-load.php');

If WordPress is in the document root add instead:

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
Salam El-Banna
  • 3,784
  • 1
  • 22
  • 34
4

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:

<?php
define("WP_ROOT", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
require_once WP_ROOT . DS . "wp-load.php";

Or

<?php
define("WP_ROOT", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
require_once WP_ROOT . DS . "wp-blog-header.php";

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.

Maxime
  • 8,645
  • 5
  • 50
  • 53
VST
  • 131
  • 1
  • 10
3
require_once(dirname(__FILE__) . '/options.php');

This is better way to include a file in WordPress.

Maxime
  • 8,645
  • 5
  • 50
  • 53
  • 4
    He wasn't asking how to include a file in Wordpress. He wanted to load Wordpress from an external PHP file. Your solution would not work. – seanbreeden Mar 30 '14 at 12:00
  • 1
    He's aware of that I'm sure. He's adding to the discussion. Now I'm going to use this method + wp-load.php. ;) – Nick Res Jun 12 '15 at 16:26
3

I use this method to load WordPress environment outside WordPress.

  if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php')) {

      /** Loads the WordPress Environment and Template */
      require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

  }
Maidul
  • 380
  • 3
  • 8
2

External files can easily access the WordPress functions. You just need to include the file wp-load.php in your external file. The wp-load.php file is located in root of your WordPress installation. Example: Suppose your file is test.php located at root directory of WordPress installation.

<?php
require_once('wp-load.php');
// Your custom code
?>

Source: How to access WordPress functions in external file

Maxime
  • 8,645
  • 5
  • 50
  • 53
Aman Dhanda
  • 438
  • 4
  • 15