WordPress page content outside WordPress

The alternative way. Problem with @david.binda solution is that:

  1. you have to hardcoding a lot of things, (manually write db credientials, table prefix..)
  2. You cannot use content filter (so if you have shortcode in your page, you will see some [something] instead of desired content..)

sure you can load wordpress environment, but…

Just yesterday I wrote an answer to output content in a file and then use it in external app.

So, in the WordPress root folder, create a subfolder named, e.g. ‘tmp’.

This folder is a sort of exchange folder from WP to your app. Be sure WordPress can write files in this folder.

In your case you can hook the save_post filter and create the file:

add_action('save_post', 'cache_page');

function cache_page( $postid ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    $filename = trailingslashit(ABSPATH) . '/tmp/page-4.inc';
    $content = apply_filters('the_content', get_post_field('post_content', 4 ) );
    file_put_contents ( $filename , $content );
}

After that in your app:

$path="wordpress/path/here/tmp/page-4.inc";
$page_content = @file_get_contents($path) ? : '';
echo $page_content;

Of course, you have to save again the page after adding the code to generate the file.


If you want with same method you can cache all the pages with a simple edit:

 function cache_page( $postid ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    $post = get_post($postid);
    if ( $post->post_type != 'page' ) return;
    $filename = trailingslashit(ABSPATH) . '/tmp/page-' . $post->ID . '.inc';
    $content = apply_filters('the_content', $post->post_content );
    file_put_contents ( $filename , $content );
}

Then in your app write a function like this

function wp_page( $id ) {
  $path="wordpress/path/here/tmp/page-" . $id . '.inc';
  $page_content = file_exists($path) ? file_get_contents($path) ? : ''; 
  echo $page_content;
}