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)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file-1/)
 * Hi
    I have some projects where I need 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 from 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. I could load it everytime,
   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. Redir to wp-admin/install.php
       function loadWP() {
           require_once 'wp-load.php';
           echo get_option( 'blogname' );
       }
       loadWP();
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [mp2300](https://wordpress.org/support/users/mp2300/)
 * (@mp2300)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file-1/#post-1888893)
 * After digging more into the problem, it seems that the messy coding style of 
   WordPress makes it impossible to include the codebase from a local scope.

Viewing 1 replies (of 1 total)

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

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 1 participant
 * Last reply from: [mp2300](https://wordpress.org/support/users/mp2300/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/load-wordpress-codebase-from-external-php-file-1/#post-1888893)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
