How to load WordPress on non WP page?

I want to use the get_option() function of wordpress in one of my theme’s files, which is not related to WP, it is just located in the same directory of the other theme files. I need this file because of some extra IPN paypal related actions, but I need some values from the options table that exists in WP to be called in this page in order for it to work. I know I can’t just use get_option() as this file is overlooked by wordpress. Is there any approach by including some parts of wordpress in my theme to make this functionality only work?

Related posts

Leave a Reply

4 comments

  1. The shortest way is to load wp-load.php and abort the loading of the template engine (Note: You couldn’t do that, if you’d be loading the header file, like you see it on many sites in the interweb).

    # No need for the template engine
    define( 'WP_USE_THEMES', false );
    # Load WordPress Core
    // Assuming we're in a subdir: "~/wp-content/plugins/current_dir"
    require_once( '../../../wp-load.php' );
    
  2. The simplest way is include wp-load.php file. This file will loads all WP core, so you can use WP functions such as get_option in your PHP file:

    include 'path/to/wp-load.php';
    
    echo get_option( 'option_name' );
    
  3. This should work on any WordPress installation:

    <?php
    function load_wp_load() {
      $wp_did_header = true;
      require_once($_SERVER['DOCUMENT_ROOT'] . '/index.php');
      $matches = preg_grep('/wp-blog-header.php/', get_included_files());
      if (!empty($matches)) {
        $abspath = dirname(reset($matches)) . '/';
        define('ABSPATH', $abspath);
        require_once(ABSPATH . 'wp-load.php');
      }
    }
    ?>