WordPress.org

Ready to get started?Download WordPress

Ideas

Enable integration with other php projects.

  1. mp2300
    Member

    12345

    I have some projects where I need to integrate other applications with WordPress. Often I need to read/write 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 has 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 makes it difficult/impossible to integrate WordPress with other PHP applications.

    Proposed solution:
    Make sure WordPress can load in local scope. The easiest way to do this, is to use the $_GLOBAL[] array each time you refere to global variables.

    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();
    Posted: 2 years ago #
  2. Gundars
    Member

    Why not to use database directly?

    Posted: 2 years ago #
  3. wjack2010
    Member

    12345

    Good suggestion :)

    Posted: 2 years ago #
  4. sdaugherty
    Member

    12345

    Actually, it would be better/safer to use a public API than to rely on the database, particularly if you are *writing* data.

    An API will hopefully fail safe, if not fail gracefully if the external application and wordpress versions get too far out of sync.

    Direct access to the database however is more dangerous, because the external application doesn't know when the schema has changed, and there are certain schema changes that will fail more spectacularly than others, possible while appearing for a while to work normally. Reading data, one of several things happens: you throw an SQL error, you get less data than you intended, you get more data (possibly including sensitive data), you get the wrong data entirely, you get no data at all, you get the data formatted differently than you expect. All of these things create undefined behavior in your application.

    Writing data directly to the database is worse. Because you aren't aware of schema changes, you may end up setting fiends to inconsistent values, creating "orphan" records that wordpress can't see, leaving out key fields, and otherwise breaking the database.

    Posted: 2 years ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    10 Votes
  • Status

    This idea is under consideration