Embed WordPress Functions Outside WordPress

From time to time you need data from WordPress, but should not displayed in the system; they are necessary outside the installation. There are basically two options. Content can be quite easy provide via feed and processed or you gain access to the system. And that is basically very simple and is briefly described here.

In an article about Google News, I have already explained how the data can be accessed, even if the output takes place in the root of the WordPress installation. Basically you can access from anywhere the data. Since WordPress 2.5 it's wp-load.php, alternatively and in older versions you simply use the wp-config.php. In addition, the wp-blog-header.php gets even earlier withdrawn and thus may be more suitable.

The following small example ties the file and output the markup and design of the active theme, therefore I use here the access to the content and settings of WordPress, since I use the functions of WordPress integration. This works, as the wp-load.php build a bridge to all other functions in WordPress is.

<?php
require( '../my_wordpress_install_root/wp-load.php' );
get_header();
 
echo 'new content outside WordPress';
 
get_footer();
?>

Depending on where the data is needed, it could be, that the absolute path of WordPress is needed. This can be determined by creating a php file with the following contents, put it in the Root of the installation and call it directly. The output is the path to the file.

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

This is then used in the require command, and can now, as usual, use the features of WordPress.

14 Comments
  1. Wow, did not know you could do this.

  2. Edwin Bollen says:

    Yes wow, I thought something like this would be possible but did not know how.

    When you write: ... and can now, as usual, use the features of WordPress.
    Does this mean that regular WP template tags can be used to display posts etc...?

    And another question: Is it possible to display the posts of two separate blogs on one page ?
    So displaying the posts of blog A besides the posts of blog B. (both separate wp installs and databases)

    Thank you for a great post

  3. Edwin Bollen says:

    It would be nice if commenters got an reply on their comment/question

  4. Michael says:

    Edwin, for example you can use your header or footer on a non WordPress page. I remember i had on my first website a handcoded homepage and under blog was the WordPress installation. i have used the same technique.
    For your second question: i think, it doesnt work. Maybe with WordPress mu, but no clue.

  5. Edwin Bollen says:

    Hi Michael,

    Thank you for your reply. I will test this on a coming project.

  6. kremalicious says:

    Interesting to see another solution. I always use a call to the wp-blog-header.php file of wordpress to get the wordpress functionality out of wordpress. Like so in the section:

    <?php
    define('WP_USE_THEMES', false);
    require('/wp-blog-header.php');
    ?>

    Do you know of the differences between both solutions and the advantages of each? I assume your solution really just calls the functionality whereas the code above loads everything or so since you have to tell to not use the wordpress theme. But I find it more handy cause I won't have to call the get_header and get_footer stuff. Now need to decide which one to use :-) All additional informations are much appreciated.

  7. Michael says:

    Since WordPress 2.6 you can use wp-load.php. Look in your wp-blog-header.php.

  8. Edwin says:

    Hi, Michael,

    I am trying to get this working, but just don't get it right.
    The first part of your example works fine. But the absolute path is given me problems.

    I want to display wordpress posts from Wordpress on domainA in a non Wordpress second domainB.

    The code '' has been put in file test.php.
    When i go to test.php this is displayed: E:\domainA.comA\wwwroot
    (wwwroot is the root directory on the server)

    How do I place this test.php in the require code ??

    Thanks for your reply

  9. Sayontan says:

    Neat. I have always created a separate file with scripts to generate the template. Part of the reason is because I have different page headers for pages outside my blog. But your suggestion is definitely worth investigating.

  10. Perri says:

    I used a similar solution which shows you how to turn your template into a Wordpress compliant theme: http://embedwordpress.com

    This I assume would offer a similar solution with half the work?

  11. bayu says:

    wow, great tips mate, i usually get wp data from feed, i'll try to do this way now, thanks

  12. Daniel Noll says:

    Silly question...does this mean that it's also possible to invoke Wordpress plugin functionality outside of the Wordpress environment also?

  13. vincent says:

    hi there,

    nice tips, really like it to mess around with wordpress.
    i've used this help to get me some extra functionality in my site.
    just as you said in your response; i've got a static html website, if you click on blog in the menu, then you go to the wordpress version of my site.

    now i've made 1 single page with every blog entry ever typed, and i'm trying to get the comments on there as well.
    weirdly enough the code for all the blog posts is working fine, but the comments simply don't show up, as if the code doesn't work.

    any ideas?

    cheers, vinnie.

  14. Ryan says:

    @Daniel - yes

5 Pings
  1. Blogging and WordPress links for 4/25/2009 | Weblog Tools Collection
  2. Webord.net
  3. Blogging and WordPress links for 4/25/2009 | The Latest Headlines
  4. Kurz und gut XII : Linktipps  miradlo bloggt  
  5. List Pages on another WP or a non-WordPress site - WordPress Tavern Forum
Leave a Reply
 
WP Engineer Tags