0

I would like to store a wordpress page as text on the server for access in a custom application. Specifically I want a div of class '.entry-content' on a page url like below

http://www.example.com/dataFeed/ or http://www.example.com/wordpress/?p=7914

I've tried a get_post() call in php like below but recieve a response of "Reload the page to get source"

<?php require_once("./wp-blog-header.php"); ?>
<?php
$post_id = 7914;
$queried_post = get_post($post_id);
echo $queried_post;
?>

Is there any way I could either of the tasks below?

• Force Wordpress to create an html version of the above page • Create a text dump of the page that resides on the server

0

One issue with your original code is that the returned $queried_post is a php object, which can't be output in whole with a simple echo. If you tried to echo a property of the object, like echo $queried_post->post_title; that might work.

That said, I'd handle this with a rewrite endpoint to keep things self-contained. The below code adds the endpoint data-feed, which when visited will output the title of the post as text/plain.

function wpd_rewrite_endpoint(){
    add_rewrite_endpoint( 'data-feed', EP_ROOT );
}
add_action( 'init', 'wpd_rewrite_endpoint' );

function wpd_parse_query( $wp ){
    if( array_key_exists( 'data-feed', $wp->query_vars ) ) {
        $post_id = 7914;
        $queried_post = get_post( $post_id );
        header( "Content-Type: text/plain" );
        echo $queried_post->post_title;
        exit;
    }
}
add_action( 'parse_query', 'wpd_parse_query' );
  • I am assuming this should be added to the wp-includes/functions.php file? What will trigger it to create the 'data-feed' endpoint? – jotamon Jan 30 '15 at 20:16
  • it should be added in a plugin, or a theme's functions.php file. nothing in wp-includes should ever be modified. I'm not sure what you mean by "what will trigger it". it registers the endpoint on init, so you can visit whatever.com/data-feed/ and the corresponding wpd_parse_query function will run on those requests. just note that you will need to flush rewrite rules for it to take effect, either by visiting the permalinks settings page, or adding a function to flush rules on plugin activation. – Milo Jan 30 '15 at 20:29

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.