PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Use WordPress Functions Externally

Posted by on 25th Jan 2009 WordPress Coding & Design 6 comments

There may be a time when you’ve added WordPress on to an already existing site, to perhaps power the news section or to just provide a blog or multiple blogs for people. If the site is small then it’s usually easier to just convert the existing pages into static pages, and use the CMS functionality of WordPress to power the whole site. However, that may not always be the case, especially with large websites.

Just because your site is external to WordPress doesn’t mean you can’t still include information and content from WordPress on the rest of your site :)

To do this all you need to do is include the following line of code at the top of any external file where you want to use the WordPress functions:

[sourcecode language="php"]include ‘path-to-wp-directory/wp-blog-header.php’;[/sourcecode]

The path to your wordpress directory needs to be relative to the page with the include in. The file wp-blog-header.php is in the root of your WordPress installation. Calling this file will automatically set the database connection to your WordPress install, and allow you to make use of any built in functions or plugin functions.

With this at the top of all pages it allows you to include additional information from WordPress such as

If you are just using WordPress to power your news section then the last option above could be useful. You could display perhaps the last 3 months using the limit parameter eg.

[sourcecode language="php"]< ?php include 'path-to-wp-directory/wp-blog-header.php'; ?>

News Archives

    < ?php wp_get_archives('type=monthly&limit=3'); ?>

[/sourcecode]

You can also include plugin functions, so you could display the recent comments on your news items or blog posts, or make use of any other useful plugin that you may have installed.

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

6 comments - Leave a reply
  • Posted by Greg on 25th Jan 2009

    Thanks Sarah, very helpful! Are there any wordpress functions that will not working outside of wordpress? What about widgets in your wordpress sidebar?

  • Posted by Sarah on 25th Jan 2009

    Hi Greg, I've never tried widgets externally as I don't use them, however as they're loaded via function (ie. dynamic_sidebar()) then I don't see why not. Easiest option is to give it a go and let us know :)

  • Posted by KJ on 16th Feb 2009

    Thanks for this. Could prove really useful for a project I'm working on :)

  • Posted by dritoch on 12th Nov 2010

    Hi is this similar to when … for example I use BP custom community theme and would like to use the bp-p2 theme functions.php file to power my index.php? Forgive me but I don't know any PHP language :)

  • Posted by Marcos on 24th Dec 2010

    VERY VERY VERY useful THANKS!

  • Posted by Lajme sot on 10th Jun 2011

    Now i can finaly access wordpress function from exterlan php file. Thank you very very much. :)
    My recent post Trafiku i organeve- BE ngre Task Forcë- HRW- Duhet një prokuror i zoti