To execute WordPress functions in an external directory (i.e., outside of the WordPress install directory), it is necessary to include a call to “wp-blog-header.php” at the top of the external file.
For example, if your WordPress-powered blog is located in a subdirectory called “blog” and the external file is in the domain root (e.g., the external file is located directly in http://domain.com/), add the following code to the top of the external file:
<?php require_once("./blog/wp-blog-header.php"); ?>
This may be generalized by writing:
<?php require($_SERVER['DOCUMENT_ROOT'].'/blog/wp-blog-header.php'); ?>