85

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

1

8 Answers 8

157

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

require_once("../../../../wp-load.php");
8
  • 5
    this is fine solution but that will load all wp site with all installed plugins and themes ... this violation of yagni principle en.wikipedia.org/wiki/You_aren't_gonna_need_it
    – HMagdy
    Commented Mar 30, 2014 at 9:46
  • 2
    This may be true if he was loading a file INSIDE of Wordpress but if you read the original question he was asking how to load it from an external php file. Commented Mar 30, 2014 at 12:00
  • 2
    so, if it isn't Yagni... what should it be? Commented Apr 7, 2016 at 13:03
  • 10
    Cleaner: require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
    – Jack
    Commented Sep 14, 2016 at 15:32
  • 4
    $_SERVER may not be defined via cli, use __DIR__ . '/path/to/wp-load,php';
    – zanderwar
    Commented Apr 25, 2018 at 7:02
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');
2
  • 8
    WordPress is not necessarily in the document root. Commented May 23, 2016 at 7:28
  • Also you need to rtrim document_root for forward slashes
    – Jack
    Commented Nov 25, 2016 at 17:03
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.

3
require_once(dirname(__FILE__) . '/options.php');

This is better way to include a file in WordPress.

2
  • 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. Commented Mar 30, 2014 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
    Commented Jun 12, 2015 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');

  }
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

0

Use chdir PHP function to change working directory.

It's need to prevent possible issues like Class not found exceptions.

$root = $_SERVER['DOCUMENT_ROOT'];

chdir($root);

require_once($root . '/wp-load.php');
0

I know this is not a direct answer to the question, but a few alternative approaches to consider that would avoid running a php file directly within WordPress completely:

1 - WP CLI

If you're needing this to run some kind of command line automation, try using WP CLI, which will load WordPress for you automatically and then let you run the php code in your file. You don't need to include wp-load.php at all when you use this method - it's already loaded for you.

wp eval-file '~/public_html/wp-content/themes/constructor/reports/testreport.php'

2 - Within Theme/Plugin Context

Ask yourself if you could possibly accomplish the task within a normal theme/plugin context. One possibility for doing something like generate a report would be to use AJAX.

wp_ajax_{$action} reference

3 - Using a scheduled task

If you want to run some php at a regular interval or at one specific time, consider using the WP Cron system. This would let you create a function within your theme or plugin and run it later at a scheduled time. Your function will run naturally in the WordPress context and won't need any special includes of wp-load.php.

https://developer.wordpress.org/plugins/cron/scheduling-wp-cron-events/ https://developer.wordpress.org/reference/functions/wp_schedule_single_event/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.