How to include WordPress functions in custom .php file?

前端 未结 6 1067
猫巷女王i
猫巷女王i 2020-11-28 05:56

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

相关标签:
6条回答
  • 2020-11-28 06:10

    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 讨论(0)
  • 2020-11-28 06:15
    require_once(dirname(__FILE__) . '/options.php');
    

    This is better way to include a file in WordPress.

    0 讨论(0)
  • 2020-11-28 06:17

    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');
    
    0 讨论(0)
  • 2020-11-28 06:23

    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.

    0 讨论(0)
  • 2020-11-28 06:23

    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');
    
      }
    
    0 讨论(0)
  • 2020-11-28 06:27

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

    require_once("../../../../wp-load.php");
    
    0 讨论(0)
提交回复
热议问题