I have a strange request. Is it possible to load the content of a Wordpress page in a normal PHP page? For example I want to display the content of the wordpress page 4 in a PHP page that is on the same domain but is not a part of the wordpress installation?


Thank you in advance


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;
  • I tried your method, the only problem is that wordpress creates a zero bytes page (page-4.inc)! – Mark Sep 23 '13 at 13:13
  • Found the typo: get_post_field('post_conteNt', 4 ) ); – Mark Sep 23 '13 at 13:16
  • @Mark so it worked, now? – gmazzap Sep 23 '13 at 13:24

I'd opt for a custom SQL query (if that really contains only post_content).

mysql_connect("hostname", "user", "password"); //use your creditials
mysql_select_db("mydb"); //use your WordPress DB name
$result = mysql_query("SELECT * FROM wp_posts"); //replace wp_ with your table prefix
while ($post = mysql_fetch_object($result)) {
    echo $post->post_title; //to get title
    echo $row->post_content; //to get content

Another option would be to load full WordPress core from your .php file, but it might be an overwhelm for getting just a post. On the other hand, this way you won't get a posibility to apply all filters which does apply in WP display.

  • Ye I thought so.....but on the wordpress page I use a shortcode for a plugin. So I thought (now....tired) to add the shortcode directly in my PHP page, and it works. But when I click on a link in that page it brings me to the wordpress page. Is there a way to open the link directly in my normal PHP page or the easy way is to use in wordpress the template of my PHP page? – Mark Sep 23 '13 at 12:44
  • Shortcodes are parsed in the_content filter. Use the link I've provided above to load WP from your external PHP file than: davidwalsh.name/wordpress-recent-posts – david.binda Sep 23 '13 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.