Title: Load WordPress codebase from external php file
Last modified: August 19, 2016

---

# Load WordPress codebase from external php file

 *  [mp2300](https://wordpress.org/support/users/mp2300/)
 * (@mp2300)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file/)
 * Short version:
    How can I include wp-load.php from inside a function in an external
   php file?
 * Long version:
    Hi I have had a couple of projects where I needed to integrate
   other applications with WordPress. Often I need to extract data from WordPress(
   posts, users, etc…) and use it in the applications.
 * The fastest way would be to fetch it directly from the database, but I prefer
   to use the WordPress functions (eg. get_userdatabylogin() ). The WordPress enviroment
   can be setup by including wp-load.php, but this have to be done in the global
   scope. If it is done inside a function, it will fail. See example at the bottom
   of the post.
 * The problem with this approach is that it is difficult to implement conditional
   loading, where WordPress is only loaded when it is needed. The alternative is
   to load it by default, but this slows down the other applications when WordPress
   is not needed.
 * So, how can I include wp-load.php from inside a function in an external php file?
   Or is there a better approach?
 * Example:
 *     ```
       // Works fine
       require_once 'wp-load.php';
       echo get_option( 'blogname' );
   
       // Does not work
       function loadWP() {
           require_once 'wp-load.php';
           echo get_option( 'blogname' );
       }
       loadWP();
       ```
   

Viewing 1 replies (of 1 total)

 *  [Rhand](https://wordpress.org/support/users/rhand/)
 * (@rhand)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file/#post-1312565)
 * Have you had any success yet?

Viewing 1 replies (of 1 total)

The topic ‘Load WordPress codebase from external php file’ is closed to new replies.

## Tags

 * [wp-load](https://wordpress.org/support/topic-tag/wp-load/)

 * 1 reply
 * 2 participants
 * Last reply from: [Rhand](https://wordpress.org/support/users/rhand/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file/#post-1312565)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
