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.


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.


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


Posted

in

by

Comments

38 responses to “Embed WordPress Functions Outside WordPress”

  1. Franklin Bishop Avatar

    Wow, did not know you could do this.

  2. Edwin Bollen Avatar

    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. […] How to embed WordPress functions outside of WordPress.: WP Engineer explains how to embed WordPress functions into code that is outside of WordPress. This can be very versatile and can help extend the usefulness of any WordPress install but would obviously require custom code. […]

  4. Edwin Bollen Avatar

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

  5. Michael Avatar

    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.

  6. Funções do WordPress fora do WordPress…

    Venho trazer algo muito legal nesse artigo, é um código relativamente pequeno para o que ele faz. Possibilita você usar as funções do WordPress fora dos arquivos do mesmo. Muito legal caso você tenha algum fórum e queira os artigos sendo e…

  7. Edwin Bollen Avatar

    Hi Michael,

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

  8. […] How to embed WordPress functions outside of WordPress.: WP Engineer explains how to embed WordPress functions into code that is outside of WordPress. This can be very versatile and can help extend the usefulness of any WordPress install but would obviously require custom code. […]

  9. […] WordPress-Funktionen außerhalb von WordPress einbinden, kann im ein oder anderen Fall praktisch sein, wenn eine Domain Website und Blog nebeneinander führt beispielsweise […]

  10. kremalicious Avatar

    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.

  11. Michael Avatar

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

  12. Edwin Avatar
    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

  13. Sayontan Avatar

    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.

  14. Perri Avatar
    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?

  15. bayu Avatar

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

  16. Daniel Noll Avatar

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

  17. […] on static pages. I did some searching on how to use wp-load.php outside of WordPress and I found a great post on WPengineer.com that was really helpful. I didn't have any problem listing the Pages of a WordPress install in a […]

  18. vincent Avatar

    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.

  19. Ryan Avatar

    @Daniel – yes

  20. S KOLLI Avatar
    S KOLLI

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

  21. Ryan Avatar

    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…

  22. TCS Avatar

    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.

  23. PACKER Avatar
    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.

  24. Frank Avatar

    @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.

  25. PACKER Avatar
    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.

  26. Paul Avatar
    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.

  27. Ludo Avatar

    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

  28. paralyzah Avatar
    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?

  29. Tom Lee Avatar
    Tom Lee

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

  30. sebastien Avatar
    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

  31. Usman Khan Avatar
    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

  32. alex Avatar
    alex

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

  33. Frank Bültge Avatar

    @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/

  34. Mark Avatar

    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.

  35. Rutwick Avatar

    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…

  36. Thiago F Macedo Avatar
    Thiago F Macedo

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

  37. Thiago F Macedo Avatar
    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

  38. Rudi Avatar

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