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.

Comments are closed.

38 comments

  1. Franklin Bishop

    Wow, did not know you could do this.

  2. Edwin Bollen

    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

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

  4. Michael

    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

    Hi Michael,

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

  6. kremalicious

    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

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

  8. Edwin

    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

    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

    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

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

  12. Daniel Noll

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

  13. vincent

    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

    @Daniel - yes

  15. S KOLLI

    If we have to get the posts from multiple wordpress blogs, how would we require the blog-header.php?

  16. Ryan

    Thanks for the tips man!One question though,will the database connection settings change according to the database details of the Worpress application?Because basically the code loads WordPress functions which I think also includes it db settings.

    So shall I reconnect to my present database after I had used the WP functions to retrieve my db settings to my main site?Just a thought...

  17. TCS

    I have pretty much the same question as Ryan. I got the blog-header called correctly, and the header will display on my external page.

    But the page has its own database, and the blog-header file sets up the DB access to the wordpress DB. Can I avoid this? Should I just reset the connection to the other DB? Any idea how to do this? I'm running into a lot of issues. Any guidance or advice in the matter would be greatly appreciated.

  18. PACKER

    Is it possible to include the content from a WordPress blog on a separate site?

    For example, you have site01.com running your WordPress blog and wish to include one of its categories on site02.com.

  19. Frank

    @PACKER: yes, you can use the wp-load.php and then all functions from WP or use the Feed of your blog and read the content from this on your Non-WP site.

  20. PACKER

    Thanks, Frank. I guess my last question would be how to get that path to work from site01.com to site02.com when you call the load page - I can't seem to get the proper URL.

  21. Paul

    Wow I think this is exactly what I needed! I have a site designed in wordpress, and I want to include the header and footer (functions and all) in my WHMCS installation (uses smarty templates). I have tried the method explained above ( exchanging <?php for {php} ), and I get the following error:

    Fatal error: Call to undefined method stdClass::set_prefix() in /home/myusername/public_html/wp-settings.php on line 287

    any idea what I am doing wrong? Any help would be appreciated. Will pay to have this done for me if you are able to. Please let me know.

  22. Ludo

    Hi !
    Looks great, but when i try to use the wp_get_current_user outside of the wordpress folder, it return 0, event if i'm logged in. The same code, inside the wordpress folder works fine.
    Shouls i include more files ?

    I'm lost.

    Ludo

  23. paralyzah

    It seems to be far from this simple. WordPress functions it seems cannot be used inside other functions or classes, or they'll produce the famous "Call to undefined method stdClass::set_prefix()" error. Also there might be problems for WordPress to include certain files some times? This might be because WP uses relative paths internally?

  24. Tom Lee

    is there some way possible to only get one widget to show, this one shows all the widgets installed?

  25. sebastien

    I use a dedicated server. The path is /var/www/vhosts/monsite.com/httpdocs
    What NEEDS I write in my file?

    If i write require('/var/www/vhosts/monsite.com/httpdocs/wp-load.php'); that doesn work
    If i write require(/vhosts/monsite.com/httpdocs/wp-load.php'); that doesn work
    If i write require('/httpdocs/wp-load.php'); that doesn work

    Is the problem in my settings server ?

    thx

  26. Usman Khan

    Whenever i try to showcase my posts outside wordpress blog an error is displayed saying unable to modify header settings output already stated at (my Website Blog Directory plugin Path) and it does not work well with the theme

  27. alex

    can i use this to fetch a wordpress user avatar to show in a hosted shoutbox like cbox?

  28. Frank Bültge

    @alex: yes, you can use the gravtars outside WP, its a gloabl service. For PHP see the documentaion: http://en.gravatar.com/site/implement/images/php/

  29. Mark

    Having a little issue with this. My footer is out of alignment just a little bit when utilizing this to make a page outside of the wordpress root. Which template file is actually being used to render the page? I think one needs to be pulled in to lineup my footer but I need to know what page wp-load.php pulls.

  30. Rutwick

    I've used this once and this doesn't work across servers/domains. If you have your WP blog in one domain/server and the page where you want your WP blog data to show up on some other domain/server, then this won't work. Say if you want to display data from some other WP blog in your webpage w/o using RSS, how are you supposed to know the absolute path? Precisely, how are you gonna target that blog? Please let me know the solution if anyone knows it...

  31. Thiago F Macedo

    At this time (3.1) that doest work with MULTISITE instance. Anybody sees a way to go?

  32. Thiago F Macedo

    I also consider important to set the constant SHORTINIT to true as it makes WP load less resources.

    Note: One should need that resources, so figure it out testing out

  33. Rudi

    Thx for this simple but useful code man, it help me a lot
    cheers

5 pingbacks

  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