Tuesday, 6 January 2015

Use WordPress Functions Externally

Advertise

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

List the Authors on the Blog List the post archives

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”]


News Archives

[/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.


No comments: