• 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)
Viewing 1 replies (of 1 total)
  • The topic ‘Load WordPress codebase from external php file’ is closed to new replies.