• 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

    (@mp2300)

    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.