What’s the easiest way to periodically (automatically) read static Markdown content into a WP page?

I’d like to sync my theme’s README.markdown into a page on my site and would love to be able to only have to update that text a single time — namely, when I commit my code up to BitBucket. I have my theme doing hg pull -u on a regular basis, so the code stays fresh and the README will get updated every time I make a change to the source, but my WordPress page containing the README then needs to get updated by hand.

(I’m using Mark Jaquith’s Markdown on Save to do the MD->HTML dance.)

Read More

Any thoughts?

Related posts

Leave a Reply

2 comments

  1. I achieved that using WP-Cron capabilities.

    add_action('wp', 'wpse_26170_activation');
    
    function wpse_26170_activation() {
        if ( !wp_next_scheduled( 'wpse_26170_update_readme_page' ) ) {
            wp_schedule_event( current_time( 'timestamp' ), 'daily', 'wpse_26170_update_readme_page');
        }
    }
    
    function wpse_26170_update_readme_page() {
        $page = array(
            'ID'           => 767,
            'post_content' => Markdown( file_get_contents( 'path/to/readme.markdown' ) )
        );
    
        if (
            // Filters return true if they existed before you removed them
            remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ) &&
            remove_filter( 'content_save_pre', 'wp_filter_post_kses' )
        ) {
            $page['post_content'] = wp_kses_post( $page['post_content'] );
        }
    
        wp_update_post($page);
    }
    

    Now this cron job should run every day updating the page with readme, please note that you have to change ‘ID’ with your page ID and path to the Markdown file.

    If you have troubles getting cron to run please read the following post from Nettuts http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/

    Also you still need Markdown On Save plugin because this function uses Markdown class from it.

    Actually you can just call the wpse_26170_update_readme_page functions after doing hg pull -u.

  2. You may parse the markdown file and show the contents in the page with some method, say with shortcode. When you update the file, it’ll automatically updated. Cause, it’ll be parsing the markdown file and serving the HTML automatically.